0

I have models.py file as follows.

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)

How do I display the updated, created fields in the Django model Admin, my admin.py file is

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)

For the above admin.py file I am getting this error.

<class 'assignments.admin.QuestionAdmin'>: (admin.E116) The value of 'list_filter[0]' refers to 'created', which does not refer to a Field.

So, how do I add an inherited fields in the django model Admin and why is the above failing?

1 Answer 1

1
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)
3
  • Thanks, using abstract works!!. but is there any way to have a mixin type structure for models too like there is for Django Views?
    – Krishna
    Commented Sep 14, 2020 at 9:06
  • @Krishna have a look at https://docs.djangoproject.com/en/2.2/ref/class-based-views/mixins-multiple-object/ and https://docs.djangoproject.com/en/2.2/topics/class-based-views/mixins/
    – Mas Zero
    Commented Sep 14, 2020 at 19:02
  • It didn't work for me
    – titusfx
    Commented Dec 28, 2021 at 11:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.