I've created a Django application and I have a model like such:
class Company(models.Model):
name = models.TextField(max_length = 200)
When I run python3 manage.py makemigrations appname
, everything works as expected, and a migration file for the class is created.
The problem happens if the class in the model defines a Meta
class as well:
class Company(models.Model):
name = models.TextField(max_length = 200)
class Meta:
app_label = "Companies"
Now, if I run the makemigrations
command, I either get a "no changes detected" message, and no migration file is generated:
> python3 manage.py makemigrations myApp
No changes detected in app 'appname'
Or, worse yet, if the model with the Meta
class had no dependencies (foreign keys), a migration is created that will drop the model/table entirely.
Or, if there is a dependency (foreign key reference from another model), I get an error like so:
matcher.OtherTable.foreignkeyref: (fields.E300) Field defines a relation with model 'Company', which is either not installed, or is abstract.
All models with a Meta
class defined will not generate a migration, either an initial one or any subsequent migration for some reason. Classes that have Meta added after the fact will be removed at the next migration. It becomes as if they don't exist at all.
I'm using Python 3.8.3 and Django 3.0.6 on Mac OS 10.15.5.
All models with a
Meta
class defined will not generate a migration, either an initial one or any subsequent migration for some reason. Classes that haveMeta
added after the fact will be removed at the next migration.
Well the problem is that you change the app_label
, o now you say that this model does not belong in the app where it belonged to intially, but somewhere else. So for that app, the models have "vanished", and for a new app (Companies
here), it appeared "out of thin air". As is specified in the documentation, one uses an app_label
when:
If a model is defined outside of an application in INSTALLED_APPS, it must declare which app it belongs to (…).
It is quite rare to specify an app_label
. If you do not specify it, it takes the name of app it is defined in.
It looks like you are however overriding the wrong setting. Based on the value you pass it, you likely want to specify the verbose_name_plural
option [Django-doc]:
class Company(models.Model):
name = models.TextField(max_length = 200)
class Meta:
verbose_name_plural = "Companies"