1

The Frobshop tutorial says: “For a deployment setup, we recommend creating product classes as data migration.”

The link points a high-level and abstract document that is more aspirational than instructive. At this point in the tutorial, no information has yet been provided on how to create a data migration. Googling and searching for this information looks like it will take many hours to figure out how to follow the recommendation.

What is the recommended way to create product classes as a data migration, in detail?

Mike Slinn
  • 7,705
  • 5
  • 51
  • 85

1 Answers1

1

The article linked to in the documentation is quite old and has since been superseded by Django's own support for data migrations, which is documented here.

The Django documentation for these does a decent job of explaining what they are and how to create them, so assuming that you've read that, then this is the sort of thing that is being suggested in the tutorial:

  1. Create a data migration in one of your project's apps:
python manage.py makemigrations --empty yourappname
  1. Add a function to this migration that populates whatever product classes you want to initialise your database with:
from django.db import migrations

def populate_product_classes(apps, schema_editor):
    ProductClass = apps.get_model('catalogue', 'ProductClass')
    
    books = ProductClass.objects.create(name='Books')
    toys = ProductClass.objects.create(name='Toys')
    # ... etc. You could also load data from a fixture here, 
    # and may also want to create attributes for each class as well, e.g., 
    ProductAttribute = apps.get_model('catalogue', 'ProductAttribute')
    ProductAttribute.objects.create(product_class=books, name='ISBN', code='isbn', type='text')


class Migration(migrations.Migration):

    dependencies = [
        ('catalogue', '0022_auto_20210210_0539'),   # This needs to be after Oscar's catalogue migrations
    ]

    operations = [
        migrations.RunPython(combine_names),
    ] 

After this has been created, executing manage.py migrate on your project will run this migration and populate the database with the product classes/attributes.

solarissmoke
  • 30,039
  • 14
  • 71
  • 73
  • The docs for [`makemigrations --empty`](https://docs.djangoproject.com/en/3.1/ref/django-admin/#django-admin-makemigrations) say "Outputs an empty migration for the specified apps, for manual editing. This is for advanced users and should not be used unless you are familiar with the migration format, migration operations, and the dependencies between your migrations." Can you explain what step 1 does in more detail? – Mike Slinn Feb 22 '21 at 15:42
  • So one of my apps would create objects for another app's model. – Mike Slinn Feb 22 '21 at 17:49
  • [This SO answer](https://stackoverflow.com/a/39743581/553865) seems to be more complex. Is the above answer likely to work in the scenarios that @Rockallite describes? – Mike Slinn Feb 22 '21 at 17:51
  • @MikeSlinn sorry, I don't have enough time at present to answer your comments thoroughly, other than to just reiterate what the Django documentation says - that you need to know what you're doing when creating your own migrations. This doesn't mean it's unsafe to do it - just that you need to understand how they work. In general, Oscar assumes that the developer will have a fairly solid understanding of Django, and be comfortable leveraging Django's capabilities to adjust its functionality. – solarissmoke Feb 23 '21 at 05:22