2

I have many classes in models.py something like:

class SomeClass1(BaseModel):
    question = models.ForeignKey(Question)
    image = models.ForeignKey(Image)

class SomeClass2(BaseModel):
    question = models.ForeignKey(Question)
    option_text = models.TextField()

Now, I want to add app_label = 'my_app_label1' to all of these classes, something like this:

class SomeClass1(BaseModel):
    question = models.ForeignKey(Question)
    image = models.ForeignKey(Image)
    class Meta:
        app_label = 'my_app_label1'

But since there are many classes, so instead of adding app_label = 'my_app_label1' to all the classes, I'm adding app_label = 'my_app_label1' to the BaseModel, like this:

class BaseModel(models.Model):
    """
    For future abstraction.
    """
    class Meta:
        app_label = 'ques_app_data'

After which I'm getting this error:

myapp.SomeClass1.basemodel_ptr: (fields.E300) Field defines a relation with model 'BaseModel', which is either not installed, or is abstract.

Can anyone please explain how to solve this ?

Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84

2 Answers2

3

Try specifying abstract=True in BaseModel inner Meta class.

class BaseModel(models.Model):
    """
    For future abstraction.
    """
    class Meta:
        abstract=True # specify this model as an Abstract Model
        app_label = 'ques_app_data'

Then inherit this BaseModel class in your model classes.

All the child model classes inherit the Meta class attributes of parent BaseModel class. Django will make one adjustment though to the Meta class of an abstract base class, before installing the Meta attribute in a child class, it sets abstract=False. This is done so that children of abstract base classes don’t automatically become abstract classes themselves.

After doing this, you will need to run the migrations again.

Note: There must be an app in your project by the name ques_app_data for this to work.

Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126
  • Yes, I did this, but after this makemigrations is not creating the tables for child classes – Saurabh Verma Jul 26 '15 at 16:29
  • What error are you getting? can you post the traceback – Rahul Gupta Jul 26 '15 at 16:34
  • No Error. Normal Execution (without extending BaseClass) Migrations for 'SomeClass1': 0003_usersubscription2.py: - Create model UserSubscription2 But while extending BaseClass, I get nothing – Saurabh Verma Jul 26 '15 at 16:36
  • Specifying a different name for `BaseModel` might help if migrations had been run without it being `abstract`. Check this link. http://stackoverflow.com/questions/7108899/using-django-south-to-move-from-concrete-inheritance-to-abstract-inheritance – Rahul Gupta Jul 26 '15 at 16:36
  • Name is not BaseModel, it's something else – Saurabh Verma Jul 26 '15 at 16:37
  • Try running all the migrations again using `migrate` command instead of `makemigrations`. Also check this similar link if it helps. http://stackoverflow.com/questions/5424262/django-permission-inheritance-problem-and-meta – Rahul Gupta Jul 26 '15 at 17:51
  • Also, there must be an app in your project by the name `ques_app_data` to work. – Rahul Gupta Jul 27 '15 at 04:45
1

This post answers this question: makemigrations not detecting changes for Extended Models in Django 1.7

having 2 major points:

1) We must have

class Meta:
        abstract = True

in BaseClass

2) app_label ques_app_data is must be included in INSTALLED_APPS

Community
  • 1
  • 1
Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84