2013-04-14 32 views
8

Tôi đang cố tạo tài nguyên (Quan sát) có 0 nhận xét không giới hạn. Tôi bị kẹt ở lỗi sau:Quan hệ One-To-nhiều-Django-ngon trong số

"error": "The model '<Observation: Observation object>' has an empty attribute 'comments' and doesn't allow a null value." 

Ngoài ra, thêm null = Đúng đối với bình luận = (...) sẽ dẫn đến các đối tượng nhận xét trống mặc dù cần có nhận xét cho các quan sát được đề cập.

Tôi cũng đã cố gắng rối tung xung quanh với đường dẫn CommentResource2 bằng cách thay đổi nó thành đường dẫn đầy đủ.

Tôi đã ở sau hướng mối quan hệ ngược từ tài liệu Tastypie của:

Reverse “Relationships”

Dưới đây là mô hình của tôi:

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' 

Và nguồn:

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() 

Trả lời

11

Bạn đang thiếu thuộc tính "nhận xét" trên mô hình Quan sát, thể thêm

observation = models.ForeignKey(Observation, related_name="comments") 

hoặc đổi sang

comments = fields.ToManyField('apps.api.CommentResource2', 'comment_set', null=True) 
+0

Thank you very much: =). – ObviousCat

Các vấn đề liên quan