Tastypie, filtering many to many relationships

心不动则不痛 提交于 2019-12-04 12:05:03

问题


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.


回答1:


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))



回答2:


Wow... I've been looking all day for this! the "attribute" is exactly what I was looking for. I almost started hacking at my models to do the filtering there out of despair.

From the Resource Field documentation for ToManyField:

Provides access to related data via a join table.

This subclass requires Django’s ORM layer to work properly.

This field also has special behavior when dealing with attribute in that it can take a callable. For instance, if you need to filter the reverse relation, you can do something like:

subjects = fields.ToManyField(SubjectResource, attribute=lambda bundle: Subject.objects.filter(notes=bundle.obj, name__startswith='Personal'))


来源:https://stackoverflow.com/questions/11557790/tastypie-filtering-many-to-many-relationships

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!