django-tastypie и «сквозные» отношения «многие ко многим»

В Django и Tastypie я пытаюсь выяснить, как правильно обращаться со многими «сквозными» отношениями со многими, например найденные здесь: https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

Вот мои примеры моделей:

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)

Теперь мой файл api.py:

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'

Я пытаюсь основывать свой код на этом примере: http://pastebin.com/L7U5rKn9

К сожалению, с этим кодом я получить эту ошибку:

"error_message": "'Ingredient' object has no attribute 'recipe'"

Кто-нибудь знает, что здесь происходит? Или как включить название ингредиента в RecipeIngredientResource? Спасибо!

РЕДАКТИРОВАТЬ:

Возможно, я сам нашел ошибку. ToManyField должен быть направлен на Ingredient, а не на RecipeIngredient. Я посмотрю, сработает ли это.

РЕДАКТИРОВАТЬ:

Новая ошибка... есть идеи? Объект имеет пустой атрибут title и не допускает значение по умолчанию или нулевое значение.

7
задан bento 17 May 2012 в 02:49
поделиться