Domanda

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?

È stato utile?

Soluzione

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)

Altri suggerimenti

Perhaps its inheritance problem, try with mixing:

http://www.linuxjournal.com/node/4540/print

If you add auto_now=True this field will be update at every modification. So it will logically normal you couldn't edit it.

If you want to add automatic date when you create an instance, I suggest you this:

from django.utils.timezone import now

class History(Article, IHasAttachments):
     date = DateField(default=now())
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top