1

如何为包含外键和日期时间字段的 django 模型提供初始数据。

例如::

仲裁/models.py

class Question(models.Model):
    user=models.ForeignKey(User)
    created=models.DateTimeField(auto_now_add=True)
    question=models.TextField()
    tags=models.CharField(max_length=50)

quorum/fixtures/questions.json

[
    {
    "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"
    }
    }
]
4

1 回答 1

0

您可以传递相关对象的主键:

"fields": {
    "question": "what is cryptography in computer science?",
    "tags": "computer science, cryptography.",
    "user': 1
}

或者,如果您的相关类定义了“自然键”,则可以只使用该字段的值。

在这种情况下,User模型定义了一个natural_key被调用的username,所以你可以通过username

"fields": {
    "question": "what is cryptography in computer science?",
    "tags": "computer science, cryptography.",
    "user': 'admin'
}

编辑 - 参考:https ://docs.djangoproject.com/en/1.5/topics/serialization/#natural-keys

于 2013-05-18T10:05:53.967 回答