Django-tastypie一对多关系 [英] Django-tastypie One-To-Many relationship

查看:139
本文介绍了Django-tastypie一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有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:

Reverse "Relationships"

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆