2012-11-19 29 views
7

Tôi đang cố gắng để có được api của tôi để cung cấp cho tôi các dữ liệu mối quan hệ đảo ngược với ngon.Tastypie Reverse Relation

Tôi có hai mô hình, DocumentContainer, và DocumentEvent, họ có liên quan như:

DocumentContainer có nhiều DocumentEvents

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

class DocumentContainerResource(ModelResource): 
    pod_events = fields.ToManyField('portal.api.resources.DocumentEventResource', 'pod_events') 
    class Meta: 
     queryset = DocumentContainer.objects.all() 
     resource_name = 'pod' 
     authorization = Authorization() 
     allowed_methods = ['get'] 

    def dehydrate_doc(self, bundle): 
     return bundle.data['doc'] or '' 

class DocumentEventResource(ModelResource): 

    pod = fields.ForeignKey(DocumentContainerResource, 'pod') 
    class Meta: 
     queryset = DocumentEvent.objects.all() 
     resource_name = 'pod_event' 
     allowed_methods = ['get'] 

Khi tôi nhấn url api của tôi, tôi nhận được lỗi sau:

DocumentContainer' object has no attribute 'pod_events 

Có ai giúp được không?

Cảm ơn.

Trả lời

1

Thay đổi dòng của bạn trong class DocumentContainerResource(...), từ

pod_events = fields.ToManyField('portal.api.resources.DocumentEventResource', 
           'pod_events') 

để

pod_events = fields.ToManyField('portal.api.resources.DocumentEventResource', 
           'pod_event_set') 

Các hậu tố cho pod_event trong trường hợp này nên _set, nhưng tùy thuộc vào tình hình, hậu tố có thể là một trong những như sau:

  • _set
  • _s
  • (không có hậu tố)

Nếu mỗi sự kiện chỉ có thể được kết hợp với lên đến một container, cũng xem xét thay đổi:

pod = fields.ForeignKey(DocumentContainerResource, 'pod') 

tới:

pod = fields.ToOneField(DocumentContainerResource, 'pod') 
+0

hmm, ngay cả sau khi thay đổi, nó không hoạt động đối với tôi. Bây giờ nó nói rằng đối tượng "DocumentContainer" không có thuộc tính 'pod_event_set' "" – rookieRailer

+0

@rookieRailer bạn có nhớ gửi các đoạn mã liên quan từ models.py của bạn không? –

+1

ForeignKey là bí danh cho ToOneField. –

12

Tôi đã thực hiện một mục blog về điều này ở đây: http://djangoandlove.blogspot.com/2012/11/tastypie-following-reverse-relationship.html.

Dưới đây là công thức cơ bản:

API.py

class [top]Resource(ModelResource): 
    [bottom]s = fields.ToManyField([bottom]Resource, '[bottom]s', full=True, null=True) 
    class Meta: 
     queryset = [top].objects.all() 

class [bottom]Resource(ModelResource): 
    class Meta: 
     queryset = [bottom].objects.all() 

Models.py

class [top](models.Model): 
    pass 

class [bottom](models.Model): 
    [top] = models.ForeignKey([top],related_name="[bottom]s") 

Nó đòi hỏi

  1. một mối quan hệ models.ForeignKey từ đứa trẻ với phụ huynh trong trường hợp này
  2. việc sử dụng một định nghĩa liên quan đến tên miền có liên quan là một thuộc tính liên quan để sử dụng related_name làm thuộc tính.
+0

Cảm ơn, điều này đã giúp tôi rất nhiều. Mặc dù tôi bỏ lỡ thuộc tính related_name = '[top]' trong ToManyField. Điều này đảm bảo dữ liệu được điền khi tạo. Xem: http://django-tastypie.readthedocs.org/en/latest/fields.html#tastypie.fields.RelatedField.related_name –