0

My question is very similar to How do I populate the Django table django_site in a yaml fixture? , but I need some help on this.

I create a group in auth_group table in my YAML fixture:

- model: auth.Group
  pk: 1
    fields:
    name: admin

In my MySql I have a Django table named: MyApp_user_groups, where MyApp is the name of the Django app; this table is automatically created by Django. This table should connect MyApp.User model (created by me) to the Groups model of Django. Using a YAML fixture, I want to add a tuple in that table (for the purpose of add a user to a group automatically when I run syncdb), but I receive the following error:

DeserializationError: Invalid model identifier: MyApp.User_Groups

So the code is

- model: MyApp.User_Groups
  pk: 1
    fields:
    user_id: 2
    group_id: 1

I'm a newbie with Django. Any ideas?

Community
  • 1
  • 1
MobileOS
  • 13
  • 6
  • did you use uppercase u or lowercase u for 'User_Groups' ? looks like a spelling error to me. what is the name of your model? it should by yourapp.yourmodel name. So pay attention to the case of U and G. – Cheng May 08 '15 at 10:03
  • MyApp_user_groups is a many to many table created by django. This table should connect MyApp.User model (created by me) to the Groups model of django. – MobileOS May 08 '15 at 10:13

1 Answers1

1

If you use Django >= 1.7 (with migrations) you could (and should) use Data Migrations. https://docs.djangoproject.com/en/1.7/topics/migrations/#data-migrations

def create_group(apps, schema_editor):
    Group = apps.get_model("auth", "Group")
    g = Group.objects.create("Your group name")
    UserGroup = apps.get_model("MyApp", "User_Group")
    UserGroup.objects.create(user_id=2, group_id=g.id)


class Migration(migrations.Migration):

    dependencies = [
        ('MyApp', '0001_initial'),
        ('auth','0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_group),
    ]
Jorick Spitzen
  • 1,559
  • 1
  • 13
  • 25