0002_fixtures.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. from django.db import migrations
  3. def create_users(apps, schema_editor):
  4. User = apps.get_model('app', 'User')
  5. to_create = []
  6. for username in ('Mal', 'Zoe', 'Wash', 'Inara', 'Jayne', 'Kaylee', 'Simon', 'River'):
  7. to_create.append(User(username=username))
  8. User.objects.bulk_create(to_create)
  9. def create_products(apps, schema_editor):
  10. ProductType = apps.get_model('app', 'ProductType')
  11. Product = apps.get_model('app', 'Product')
  12. products = {
  13. 'food': ['apple', 'meat', 'banana'],
  14. 'weapon': ['blaster', 'gun', 'knife'],
  15. 'starships': ['Serenity'],
  16. }
  17. to_create = []
  18. for key, items in products.items():
  19. product_type = ProductType.objects.create(name=key)
  20. for product in items:
  21. to_create.append(Product(name=product, product_type=product_type))
  22. Product.objects.bulk_create(to_create)
  23. class Migration(migrations.Migration):
  24. dependencies = [
  25. ('app', '0001_initial'),
  26. ]
  27. operations = [
  28. migrations.RunPython(create_users, migrations.RunPython.noop),
  29. migrations.RunPython(create_products, migrations.RunPython.noop),
  30. ]