Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use an inheriting Django model's Meta class to configure a field defined in an inherited abstract model

I would like to use properties from an inheriting model's Meta class to configure a field defined in an abstract model higher up the inheritance tree:

class NamedModel(models.Model):
    class Meta:
        abstract = True
        verbose_name = 'object'

    name = models.CharField("Name",
        max_length=200,
        db_index=True,
        help_text="A meaningful name for this %s." % Meta.verbose_name)
        # see what I'm trying to do here?
    )
    ...

class OwnedModel(NamedModel):
    class Meta(NamedModel.Meta):
        verbose_name = 'owned object'

I would like the help text on the name field of OwnedModel forms to say 'A meaningful name for this owned object'. But it does not: the word 'owned' is missing, which would suggest that the verbose_name from the NamedModel.Meta is used when the model is set up, not OwnedModel.Meta.

This isn't quite what I expect from an inheritance point of view: is there some way to get the field to be created whereby Meta.verbose_name refers to the value on the non-abstract model class, not the abstract one on which the field was defined?

Or am I being daft?

(This may seem like a trivial example, and it is: but it's just to illustrate the point of something more important and complex I am trying to do)

Many thanks in advance.

like image 281
James Pearce Avatar asked Sep 01 '09 10:09

James Pearce


People also ask

Can we inherit models in Django?

Model Inheritance in Django works almost identically to the way normal class inheritance works in python. In this article we will revolve around how to create abstract base class in Django Models. Abstract Base Class are useful when you want to put some common information into a number of other models.

What are the inheritance style in Django?

Multi-table inheritance: This inheritance style is used if you want to subclass on an existing model and each of the models to have its own database table. Proxy models: This inheritance style allows the user to modify the python level behavior without actually modifying the model's field.

How can create primary key in Django model?

If you'd like to specify a custom primary key, specify primary_key=True on one of your fields. If Django sees you've explicitly set Field.primary_key , it won't add the automatic id column. Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).

What is abstract model Django?

An abstract model is a base class in which you define fields you want to include in all child models. Django doesn't create any database table for abstract models. A database table is created for each child model, including the fields inherited from the abstract class and the ones defined in the child model.


1 Answers

Why don't you try to make a class.

class BaseNamedModelMeta:
    abstract = True
    verbose_name = "your text"

And then inherit and override whatever you want like this:

class OwnedModel(NamedModel):
    class Meta(BaseNamedModelMeta):
        verbose_name = 'owned object'
like image 127
Fer Mena Avatar answered Nov 15 '22 09:11

Fer Mena