Django-tastypie一对多关系 [英] Django-tastypie One-To-Many relationship
问题描述
我正在尝试创建一个具有0到无限制注释的资源(Observation)。我被困在以下错误中:
error:模型<观察:观察对象>一个空属性'comments',不允许空值。
另外,将null = True添加到comments =(...)甚至会导致空的注释对象尽管应该对有意见的意见进行评论。
我也尝试通过将CommentResource2路径更改为完整路径来解决问题。
我一直在追踪Tastypie文档的反向关系指南:
这是我的模型: p>
class Observation(ObsModel):
taxon_node = models.ForeignKey(TaxonNode,related_name ='observer_taxon_node',null = True)
substrate = models.ForeignKey(TaxonNode,related_name ='observation_substrate',null = True,blank = True)
source = models.CharField(max_length = 255,blank = True)
sample = models.ForeignKey(Sample)
评论= models.TextField(blank = True)
exact_time = models.DateTimeField(null = True)
individual_count = models.IntegerField(null = True)
is_verified = models.NullBooleanField(null = True )
verified_by = models.ForeignKey(User,null = True)
verified_time = models.DateTimeField('time verified',null = True)
class Meta():
app_label ='观察'
类注释(models.Model):
观察= models.ForeignKey(观察)
comment = models.TextField )
created_time = models.DateTimeField('time created',auto_now_add = True,editable = False)
class Meta:
app_label ='observation_moderate'
资源:
class ObservationResource2(ModelResource):
comments = fields.ToManyField('apps.api.CommentResource2','comments')
class Meta:
queryset = Observatio n.objects.filter(is_verified = False)
authentication = SessionAuthentication()
authorization = DjangoAuthorization()
resource_name ='观察'
class CommentResource2(ModelResource) :
观察= fields.ToOneField(ObservationResource2,'观察')
class Meta:
queryset = Comment.objects.all()
resource_name ='comments'
认证= SessionAuthentication()
授权= DjangoAuthorization()
您在观察模型中缺少评论属性,
添加
observe = models。 ForeignKey(Observation,related_name =comments)
或更改为
comments = fields.ToManyField('apps.api.CommentResource2','comment_set',null = True)
/ pre>
I'm trying to create a resource (Observation) that has 0 to unlimited comments. I'm stuck at the following error:
"error": "The model '<Observation: Observation object>' has an empty attribute 'comments' and doesn't allow a null value."
Also, adding null=True to comments = (...) will result in empty comment objects even though there should be comments for observations in question.
I've also tried messing around with CommentResource2 path by changing it to full path.
I've been following the reverse relationship guide from Tastypie's documentation:
Here are my models:
class Observation(ObsModel): taxon_node = models.ForeignKey(TaxonNode, related_name = 'observation_taxon_node', null = True) substrate = models.ForeignKey(TaxonNode, related_name = 'observation_substrate', null = True, blank=True) source = models.CharField(max_length=255, blank=True) sample = models.ForeignKey(Sample) remarks = models.TextField(blank = True) exact_time = models.DateTimeField(null=True) individual_count = models.IntegerField(null = True) is_verified = models.NullBooleanField(null = True) verified_by = models.ForeignKey(User, null = True) verified_time = models.DateTimeField('time verified', null = True) class Meta(): app_label = 'observation' class Comment(models.Model): observation = models.ForeignKey(Observation) comment = models.TextField() created_time = models.DateTimeField('time created', auto_now_add=True, editable=False) class Meta: app_label = 'observation_moderate'
And resources:
class ObservationResource2(ModelResource): comments = fields.ToManyField('apps.api.CommentResource2', 'comments') class Meta: queryset = Observation.objects.filter(is_verified=False) authentication = SessionAuthentication() authorization = DjangoAuthorization() resource_name = 'observation' class CommentResource2(ModelResource): observation = fields.ToOneField(ObservationResource2, 'observation') class Meta: queryset = Comment.objects.all() resource_name = 'comments' authentication = SessionAuthentication() authorization = DjangoAuthorization()
解决方案You are missing the "comments" attribute on the Observation model, either add
observation = models.ForeignKey(Observation, related_name="comments")
or change to
comments = fields.ToManyField('apps.api.CommentResource2', 'comment_set', null=True)
这篇关于Django-tastypie一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!