Tastypie, filtering many to many relationships
django, python, tastypie
Solution
You can filter fields using lambda bundle attribute showing table name and field name.
tags = fields.ToManyField('django_app.api.TagsResource', attribute=lambda bundle: bundle.obj.tags.filter(tags__deleted=0))
Problem
I have two models that are linked by another model through a many to many relationship. Here's the models themselves ``` class Posts(models.Model): id = models.CharField(max_length=108, primary_key=True) tags = models.ManyToManyField('Tags', through='PostTags') class Tags(models.Model): id = models.CharField(max_length=108, primary_key=True) posts = models.ManyToManyField('Posts', through='PostTags') class PostTags(models.Model): id = models.CharField(max_length=108, primary_key=True) deleted = models.IntegerField() post_id = models.ForeignKey('Posts', db_column='post_field') tag_id = models.ForeignKey('Tags', db_column='tag_field') ``` And the tastypie resources ``` class PostsResource(ModelResource): tags = fields.ToManyField('django_app.api.TagsResource', 'tags', null=True) class Meta: queryset = Posts.objects.filter(deleted=0) resource_name = 'posts' class TagsResource(ModelResource): posts = fields.ToManyField('django_app.api.PostsResource', 'posts', null=True) class Meta: queryset = Tags.objects.filter(deleted=0) resource_name = 'tags' ``` On the posttags table there is a deleted flag, is it possible to only return linked results when the deleted flag in PostTags is 0? I have tried this filter attribute in tastypie but it only seems to care about the flag in the linked table(ie tags or posts) not the actual table doing the linking.