在 Django ModelAdmin 中显示继承的字段
Display an inherited Field in Django ModelAdmin
我有 models.py 文件如下。
from django.db import models
class UpdateCreateModelMixin:
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Question(UpdateCreateModelMixin, models.Model):
name = models.CharField(max_length=100)
code = ...
... (Some more field)
如何在 Django 模型管理中显示更新、创建的字段,我的 admin.py 文件是
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
list_display = ('name', 'created', 'updated')
list_filter = ('created')
admin.site.register(Question, QuestionAdmin)
对于上述 admin.py 文件,我收到此错误。
<class 'assignments.admin.QuestionAdmin'>: (admin.E116) The value of 'list_filter[0]' refers to 'created', which does not refer to a Field.
那么,我如何在 django 模型管理中添加一个继承的字段,为什么上面的操作失败了?
class UpdateCreateModelMixin(models.Model):
class Meta:
abstract = True
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Question(UpdateCreateModelMixin):
name = models.CharField(max_length=100)
code = ...
... (Some more field)
我有 models.py 文件如下。
from django.db import models
class UpdateCreateModelMixin:
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Question(UpdateCreateModelMixin, models.Model):
name = models.CharField(max_length=100)
code = ...
... (Some more field)
如何在 Django 模型管理中显示更新、创建的字段,我的 admin.py 文件是
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
list_display = ('name', 'created', 'updated')
list_filter = ('created')
admin.site.register(Question, QuestionAdmin)
对于上述 admin.py 文件,我收到此错误。
<class 'assignments.admin.QuestionAdmin'>: (admin.E116) The value of 'list_filter[0]' refers to 'created', which does not refer to a Field.
那么,我如何在 django 模型管理中添加一个继承的字段,为什么上面的操作失败了?
class UpdateCreateModelMixin(models.Model):
class Meta:
abstract = True
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Question(UpdateCreateModelMixin):
name = models.CharField(max_length=100)
code = ...
... (Some more field)