python - How to populate a django database with users -


i want populate django database using manage.py loaddata initial_data.json json file contains specifications of several objects. problem these object have 'user' attribute referencing django user object, indicate user created them. model these objects looks this:

from django.db import models django.contrib.auth.models import user  class myspecialmodel(models.model):     name = models.charfield(max_length=200)     user = models.foreignkey(user) 

the objects in json this:

[{"fields":{             "name": "some name",             "user": xxx        }  ...] 

the problem don't know write in place of xxx indicate user. have tried user names django tells me expects number xxx are. using number not produce bug don't see database populated. there way place django object in initial_data.json file ?

you should write user.pk there, integer value.

[{"fields":{             "name": "some name",             "user": 1        }  ...] 

obviously, user pk should created before import object foreign key it, maybe easier create (fake or not) users through admin before , export with: python manage.py dumpdata auth.user --indent 4 > users.json


Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -