django-tastypie and many to many "through" relationships
Asked Answered
D

2

7

In Django and Tastypie I'm attempting to figure out how to properly deal with Many to Many "through" relationships, like those found here: https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

Here are my sample models:

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)

Now my api.py file:

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'

I'm trying to base my code on this example: http://pastebin.com/L7U5rKn9

Unfortunately, with this code I get this error:

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

Does anyone know what's happening here? Or how I can include the name of the ingredient in the RecipeIngredientResource? Thanks!

EDIT:

I may have found the error myself. ToManyField should be directed toward Ingredient and not RecipeIngredient. I'll see if this does the job.

EDIT:

New error.. any ideas? The object '' has an empty attribute 'title' and doesn't allow a default or null value.

Drench answered 17/5, 2012 at 2:12 Comment(0)
P
3

You mentioned:

I may have found the error myself. ToManyField should be directed toward Ingredient and not RecipeIngredient. I'll see if this does the job.

There's a better approach though [Tastypie M2M](http://blog.eugene-yeo.in/django-tastypie-manytomany-through.html) (old blog is offline: https://github.com/9gix/eugene-yeo.in/blob/master/content/web/django-tastiepie-m2m.rst)

In short summary, Instead of ToManyField to Ingredients, I use ToManyField toward the ThroughModel. And customize the attribute kwargs to be a callback function that return the ThroughModel Queryset.

Update (2014 Apr)

This answer is made long ago. Not sure if it is still useful.

Poet answered 3/12, 2012 at 18:21 Comment(8)
Please include the important points of the answer here, on this post. Stack Overflow is not here to be a repository of links to stuff, but to be a repository of answers. This is also a very inappropriate way to promote your blog.Wikiup
Link seems to be dead at the momentRant
The link is still dead, and I wasn't able to find a cached version anywhere.Catboat
@JayTaylor, Link is updated. Hope that's help you. btw, I am no longer using Tastypie, I use DjangoRestFramework (DRF) instead.Poet
can you post an example here please?? the link is not working anymoreRebarebah
@Rebarebah Sorry for the inconvenience github.com/9gix/eugene-yeo.in/blob/master/content/web/…Poet
hey @Poet will your solution allow me to POST/PATCH my resources without the need for me to override the save_m2m() method?? If not would you be kind enough to provide me with an example on how i can achieve that? I have been struggling with this for days now. ThanksRebarebah
If I'm not wrong, I didn't tried the POST/PATCH for this relationships. It has been many years that I haven't touch Tastypie, I am sorry that I can't help you. However, it would be good if you have a look at how DRF solve the M2M POST PATCH issues at the moment, hope that helps.Poet
P
-2

I had the same issue as you. To solve it, I simply just removed the ToMany field (like in RecipeResource) from the API. This worked for us because the model still had the the manytomany field (just not the API), and you could still query the relation by querying the intermediate model instead.

Pallid answered 5/6, 2012 at 20:1 Comment(1)
This doesn't really answer the question.Spy

© 2022 - 2024 — McMap. All rights reserved.