Opened 8 years ago
Last modified 8 years ago
#27168 closed Bug
"LookupError: App 'xxxxx' doesn't have a 'xxxxx' model" with custom table name — at Initial Version
Reported by: | John | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 1.8 |
Severity: | Normal | Keywords: | migration |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
I recently wrote a migration script in order to migrate some date and one of the model uses a custom table name using db_table (let's call it "myapp_my_custom_table")
When I try to run the migration I got this error:
LookupError: App 'myapp' doesn't have a 'mymodel' model
After investigating, I found out that the model name MyModel (not the real one) is put in lower case then used as key to fetch the actual model class (
django.apps.config.AppConfig#get_model )
One of the item of the self.models is named after the db_table my_custom_table, instead of the model name mymodel
The solution I found is to pass the table name instead of the model name.
Before:
def migrate_data(apps, schema_editor): mymodel_model = apps.get_model( app_label='myapp', model_name='MyModel')
After:
def migrate_data(apps, schema_editor): mymodel_model = apps.get_model( app_label='myapp', model_name='my_custom_table')