Search code examples
pythondjangodjango-grappellidjango-tinymcedjango-filebrowser

django model inheritance and admin application


I have a django application in which I want to use model inheritance. The application contains one super model class Article and here is its code

class Article(models.Model):
    english_title = CharField(max_length=200)
    arabic_title = CharField(max_length=200)
    english_body = HTMLField()
    arabic_body = HTMLField()
    enabled = BooleanField()

    def __unicode__(self):
        return self.english_title

    def get_body(self, locale):
        if locale == "ar" :
            return self.arabic_body
        else:
            return self.english_body

    def get_title(self, locale):
        if locale == "ar" :
            return self.arabic_title
        else:
            return self.english_title

and there is a child class called History which extends this class and here is its code

class History(Article, IHasAttachments):
     date = DateField(auto_now_add=True)

My problem appears in the admin application where the dateField (date) in the History model does not appear in the admin form when inserting new entry.

NOTE: I am using django-tinymce, djnago-filebrowser, and django-grappelli

What would be the problem?


Solution

  • I think the problem is in your History model you set auto_now_add=True, which will prevent your date field shown on admin, please check the django document about Model field reference:

    As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set.

    And about Field.editable:

    If False, the field will not be displayed in the admin or any other ModelForm.

    If you want it editable but also has a default value, try this:

    class History(Article, IHasAttachments):
        date = DateField(default=datetime.date.today)