[英]Setting up foreignkeys in loaddata for Django
我设置了两个类,其中用户可能具有多个访问键。
class User(models.Model):
first_name = models.CharField(max_length=50)
middle_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
password = models.CharField(max_length=50)
birthday = models.DateField()
def __str__(self):
return self.first_name+" "+self.last_name
class pets(models.Model):
user = models.ForeignKey('User')
type = models.CharField(max_length=50)
color = models.CharField(max_length=50)
def __str__(self):
return self.type
我正在尝试通过一个看起来像json文件的loaddata使用数据预加载表:
[
{
"fields": {
"first_name": "John",
"last_name": "Doe",
"middle_name": "G",
"birthday": "1900-07-21",
"password": "goforit123",
"email": "John.Doe@gmail.com"
},
"model": "account_data.user",
"pk": 1
},
{
"fields": {
"user": "????"
"type": "dog",
"color": "blue"
},
"model": "account_data.pets",
"pk": 1
}
]
我要为宠物课程向用户添加什么?
非常感谢!
您只需要使用要链接的对象的de pk:
[
{
"fields": {
"first_name": "John",
"last_name": "Doe",
"middle_name": "G",
"birthday": "1900-07-21",
"password": "goforit123",
"email": "John.Doe@gmail.com"
},
"model": "account_data.user",
"pk": 1 // This is the pk you have to use
},
{
"fields": {
"user": 1, // Use the pk, in this case you're referencing to John Doe
"type": "dog",
"color": "blue"
},
"model": "account_data.pets",
"pk": 1
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.