Search code examples
djangodjango-oscar

django-oscar2.1 giving errors on is_public field. Its a model loading issue


I am migrating to django-oscar2.1

I have forked catalogue app, it has some existing migrations. Newly added field is_public is in migration #10. I have a custom migration #5 which is using create_from_breadcrumbs method. So when I tried to run migrate on fresh db migration#5 trigger error is_public field not exists in db.

create_from_breadcrumbs Oscar loads a model with latest state which contains is_public in models but not in db.

In migration #5 I am loading model like this Category = apps.get_model("catalogue", "Category")


Solution

  • You cannot use create_from_breadcrumbs in a migration like that. The reason for this is explained in Django's documentation on migrations - that the version of the model used in external code may be different from the one that this migration expects - which is why it fails.

    If you want to create model objects as part of your migration you need to use the historic version of the model in the migration. This means defining your own version of create_from_breadcrumbs, and using the historic version of the model as suggested by the Django documentation:

    def create_from_breadcrumbs_for_migration(apps, schema_editor):
        Category = apps.get_model('catalogue', 'Category')
        # Perform logic to create categories here.
    
    
    class Migration(migrations.Migration):
    
        dependencies = [
            ('catalogue', '0004_xxxx'),
        ]
    
        operations = [
            migrations.RunPython(create_from_breadcrumbs_for_migration),
        ]
        
    

    That said, I would argue that migrations are not meant for this sort of data population in the first place. You would in my view be better off writing a separate management command for this, which you run after the migrations have been applied.