H C*_*H C 3 django json django-models python-2.7
我设置了两个类,其中一个用户可能有多个访问密钥。
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
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 loaddata 通过如下所示的 json 文件预加载包含数据的表:
[
{
"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
}
]
Run Code Online (Sandbox Code Playgroud)
我在宠物类的用户中放了什么?
非常感谢!
您只需要使用要链接的对象的 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
}
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1678 次 |
| 最近记录: |