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.
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
@rookieRailer bạn có nhớ gửi các đoạn mã liên quan từ models.py của bạn không? –
ForeignKey là bí danh cho ToOneField. –