import datetimefrom django.utils import timezonefrom django.db import modelsclass Poll(models.Model): question = models.CharFIEld(max_length = 200) pub_date = models.DateTimeFIEld('date published') def __unicode__(self): return self.question def was_published_recently(self): Now = timezone.Now() return Now - datetime.timedelta(days = 1) <= self.pub_date < Now was_published_recently.admin_order_fIEld = 'pub_date' was_published_recently.boolean = True was_published_recently.short_description = 'Published recently?'class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharFIEld(max_length = 200) Votes = models.IntegerFIEld(default = 0) def __unicode__(self): return self.choice_text
Choice使用ForeignKey,这是一种多对一的关系,所以我应该能够使用一个选择进行多个轮询.如果我尝试从夹具中加载它,如下所示:
[ { "model": "polls.Poll","pk": 3,"fIElds": { "question": "What time do you sleep?","pub_date": "2013-07-29T10:00:00+00:00" } },{ "model": "polls.Poll","pk": 4,"fIElds": { "question": "What time do you get up?",{ "model": "polls.Choice","fIElds": { "poll": [{"pk": 3},{"pk": 4}],"choice_text": "10:00","Votes": 0 } }]
我收到此错误:
DeserializationError: Problem installing fixture '/.../poll/polls/fixtures/initial_data.Json': [u"'[{u'pk': 3},{u'pk': 4}]' value must be an integer."]
或者:
{ "model": "polls.Choice","fIElds": { "poll": [3,4],"Votes": 0 } }
我收到此错误:
DeserializationError: Problem installing fixture '/.../poll/polls/fixtures/initial_data.Json': [u"'[3,4]' value must be an integer."]
如何从夹具中加载多对一关系?
解决方法 这是教程中的引用:Finally,note a relationship is defined,using ForeignKey. That tells
Django each Choice is related to a single Poll. Django supports all
the common database relationships: many-to-ones,many-to-manys and
one-to-ones.
每个Choice都与一个Poll相关,并且您尝试将一个键列表传递给Choice.poll字段.
但是,每次民意调查都可以与几种选择相关:
{ "pk": 4,"model": "polls.Choice","fIElds": { "Votes": 0,"poll": 2,"choice_text": "Meh." }},{ "pk": 5,"choice_text": "Not so good." }}
希望有所帮助.
总结以上是内存溢出为你收集整理的在Django中加载具有多对一关系的夹具全部内容,希望文章能够帮你解决在Django中加载具有多对一关系的夹具所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)