如何为django模型提供初始数据,包括foreignkeys和datetimefield。你知道吗
例如:
class Question(models.Model):
user=models.ForeignKey(User)
created=models.DateTimeField(auto_now_add=True)
question=models.TextField()
tags=models.CharField(max_length=50)
[
{
"model": "quorum.question",
"pk": 1,
"fields": {
"question": "what is cryptography in computer science?",
"tags": "computer science, cryptography."
}
},
{
"model": "quorum.question",
"pk": 2,
"fields": {
"question": "How python language got name? from snake? is it from monty python circus performence(the projectile stuff)?",
"tags": "python"
}
}
]
您可以传递相关对象的主键:
或者,如果相关类定义了一个“自然键”,则可以只使用该字段的值。你知道吗
在本例中,
User
模型定义了一个名为username
的natural_key
,因此您可以只传递username
编辑-引用:https://docs.djangoproject.com/en/1.5/topics/serialization/#natural-keys
相关问题 更多 >
编程相关推荐