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?

Krishna
  • 229
  • 1
  • 2
  • 7

1 Answers1

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)
Mas Zero
  • 503
  • 3
  • 16
  • 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 Sep 14 '20 at 09: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 Sep 14 '20 at 19:02
  • It didn't work for me – titusfx Dec 28 '21 at 11:51