samedi 5 avril 2014

Django - modèles d'administration ne reconnaissent pas les champs hérités - Stack Overflow


I get a FieldError with the following "Exception Value: Unknown field(s) (creation_date) specified for Module. Check fields/fieldsets/exclude attributes of class ModuleAdmin" if I use the admin interface to get the following model "Module":


class GeneralModel(models.Model):
creation_date = models.DateTimeField('date of creation', auto_now_add=True)
edited_date = models.DateTimeField('date of last modification', auto_now=True)

class Meta:
abstract = True


class Module(GeneralModel):
name = models.CharField(max_length=100)
shortDescription = models.CharField("summary", max_length=100)
description = models.CharField("description", max_length=1500)
authors = models.ManyToManyField("Author", through="Authorship")

def __unicode__(self):
return self.name

With the following ModelAdmin Code:


class ModuleAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
formfield = super(ModuleAdmin, self).formfield_for_dbfield(db_field, **kwargs)
if db_field.name == 'description':
formfield.widget = forms.Textarea(attrs=formfield.widget.attrs)
return formfield
fieldsets = [
("General", {"fields": ["name", "shortDescription"]}),
("Details", {"fields": ["description", "creation_date"], "classes": ["collapse"]})
]


I get a FieldError with the following "Exception Value: Unknown field(s) (creation_date) specified for Module. Check fields/fieldsets/exclude attributes of class ModuleAdmin" if I use the admin interface to get the following model "Module":


class GeneralModel(models.Model):
creation_date = models.DateTimeField('date of creation', auto_now_add=True)
edited_date = models.DateTimeField('date of last modification', auto_now=True)

class Meta:
abstract = True


class Module(GeneralModel):
name = models.CharField(max_length=100)
shortDescription = models.CharField("summary", max_length=100)
description = models.CharField("description", max_length=1500)
authors = models.ManyToManyField("Author", through="Authorship")

def __unicode__(self):
return self.name

With the following ModelAdmin Code:


class ModuleAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
formfield = super(ModuleAdmin, self).formfield_for_dbfield(db_field, **kwargs)
if db_field.name == 'description':
formfield.widget = forms.Textarea(attrs=formfield.widget.attrs)
return formfield
fieldsets = [
("General", {"fields": ["name", "shortDescription"]}),
("Details", {"fields": ["description", "creation_date"], "classes": ["collapse"]})
]

0 commentaires:

Enregistrer un commentaire