Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admin Models do not recognize inherited fields

I am wondering if this is something specific to django.admin, django, or even python? But I don't really understand the point in having abstract super classes if I cannot access their fields :-). Have I done something wrong?

Example: 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"]})
    ]
like image 746
Antoine Lizée Avatar asked Mar 27 '14 08:03

Antoine Lizée


People also ask

Why are my discriminator values not being materialized in Entity Framework?

When materializing results from a query, if we come across a discriminator value, which isn't mapped to any entity type in the model, we throw an exception since we don't know how to materialize the results. This error only occurs if your database contains rows with discriminator values, which aren't mapped in the EF model.

Can the discriminator be mapped to a regular Entity Framework property?

Finally, the discriminator can also be mapped to a regular .NET property in your entity: When querying for derived entities, which use the TPH pattern, EF Core adds a predicate over discriminator column in the query.

How do I setup inheritance in EF?

EF will only setup inheritance if two or more inherited types are explicitly included in the model. EF will not scan for base or derived types that were not otherwise included in the model. You can include types in the model by exposing a DbSet<TEntity> for each type in the inheritance hierarchy.

Why is the discriminator mapping in EF Core Model incomplete?

This error only occurs if your database contains rows with discriminator values, which aren't mapped in the EF model. If you have such data, then you can mark the discriminator mapping in EF Core model as incomplete to indicate that we should always add filter predicate for querying any type in the hierarchy.


1 Answers

Your problem is related to the answer: https://stackoverflow.com/a/3594927/3006165

The error is due to date having auto_now_add=True (or auto_now=True). As the value is automatic, it's not editable, so it's not in the form

According to the documentantion:

The fields option ... may contain callables only if they are listed in readonly_fields.

like image 199
RMotitsuki Avatar answered Sep 25 '22 15:09

RMotitsuki