Trong Django và Tastypie Tôi đang cố gắng tìm ra cách để đối phó đúng với nhiều nhiều "thông qua" các mối quan hệ, giống như những người tìm thấy ở đây: https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationshipsdjango-tastypie và nhiều nhiều "thông qua" các mối quan hệ
Dưới đây là mô hình mẫu của tôi:
class Ingredient(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
class RecipeIngredients(models.Model):
recipe = models.ForeignKey('Recipe')
ingredient = models.ForeignKey('Ingredient')
weight = models.IntegerField(null = True, blank = True)
class Recipe(models.Model):
title = models.CharField(max_length=100)
ingredients = models.ManyToManyField(Ingredient, related_name='ingredients', through='RecipeIngredients', null = True, blank = True)
Bây giờ tập tin api.py tôi:
class IngredientResource(ModelResource):
ingredients = fields.ToOneField('RecipeResource', 'ingredients', full=True)
class Meta:
queryset = Ingredient.objects.all()
resource_name = "ingredients"
class RecipeIngredientResource(ModelResource):
ingredient = fields.ToOneField(IngredientResource, 'ingredients', full=True)
recipe = fields.ToOneField('RecipeResource', 'recipe', full=True)
class Meta:
queryset= RecipeIngredients.objects.all()
class RecipeResource(ModelResource):
ingredients = fields.ToManyField(RecipeIngredientResource, 'ingredients', full=True)
class Meta:
queryset = Recipe.objects.all()
resource_name = 'recipe'
tôi đang cố gắng để căn mã của tôi trên ví dụ này: http://pastebin.com/L7U5rKn9
Thật không may, với mã này tôi nhận được lỗi này:
"error_message": "'Ingredient' object has no attribute 'recipe'"
Có ai biết những gì đang xảy ra ở đây? Hoặc làm thế nào tôi có thể bao gồm tên của thành phần trong RecipeIngredientResource? Cảm ơn!
EDIT:
Tôi có thể đã tự tìm thấy lỗi. ToManyField nên được hướng đến thành phần và không RecipeIngredient. Tôi sẽ xem điều này có thực hiện được không.
EDIT:
Lỗi mới .. bất kỳ ý tưởng nào? Đối tượng '' có thuộc tính trống 'tiêu đề' và không cho phép giá trị mặc định hoặc giá trị rỗng.
Vui lòng bao gồm các điểm quan trọng của câu trả lời ở đây, trên bài đăng này. Stack Overflow không phải là một kho chứa các liên kết đến các công cụ, mà là một kho chứa các câu trả lời. Đây cũng là một cách rất không phù hợp để quảng bá blog của bạn. –
Duh, Ok, .. Tôi sẽ sao chép câu trả lời ở đây ... Vì bài đăng quá dài ... – Yeo
Liên kết dường như đã chết tại thời điểm – msc