Посеять данные, используя пустую ошибку миграции django

У меня есть миграция семян для моей модели плана, и она связана с разрешениями django:

def seed_pro_plan(apps, schema_editor):
    
    Plan = apps.get_model('billing', 'Plan')
    Feature = apps.get_model('billing', 'Feature')
    BasePermission = apps.get_model('auth', 'Permission')

    plan = Plan()
    plan.name = "Pro"
    plan.monthlyPrice = 19
    plan.annualPrice = 228
    plan.description = "Pro Plan"
    plan.isRecommended = True
    plan.auctionProductLimit = 100
    plan.save()

    permission1 = BasePermission.objects.get(codename='can_set_exclude_product', content_type__app_label='billing') 
    permission2 = BasePermission.objects.get(codename='can_set_schedule', content_type__app_label='billing')

    plan.permissions.add(permission1)
    plan.permissions.add(permission2)
    

    f1 = Feature()
    f1.title = "Create unlimited campaigns"
    f1.description = "Create unlimited campaigns"
    f1.order = 1
    f1.save()
    plan.top_features.add(f1)

    f2 = Feature()
    f2.title = "Schedule campaigns with start and end date"
    f2.description = "Schedule campaigns with start and end date"
    f2.order = 2
    f2.save()
    plan.top_features.add(f2)

    f3 = Feature()
    f3.title = "Use exclusion for product selection at campaign definition"
    f3.description = "Use exclusion for product selection at campaign definition"
    f3.order = 3
    f3.save()
    plan.top_features.add(f3)

    plan.save()


class Migration(migrations.Migration):

    dependencies = [
        ('billing', '0006_plan_isrecommended'),
    ]

    operations = [
        migrations.RunPython(seed_free_plan),
        migrations.RunPython(seed_pro_plan),
        migrations.RunPython(seed_ultimate_plan),
    ]

Когда я запускаю этот код миграции, он не работает в первый раз и выдает ошибку:

Запрос, соответствующий разрешению, не существует

Но когда я запускаю его как поддельную миграцию, а затем возвращаю его в реальную миграцию, он работает.

Каково решение этой проблемы?

Вернуться на верх