Tastypie,过滤许多关系 [英] Tastypie, filtering many to many relationships

查看:93
本文介绍了Tastypie,过滤许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们有两个模型通过多对多关系链接到另一个模型中。


$ b $





标签= models.ManyToManyField('


class标签(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 ')

而且tastypie资源

  class PostsResource(ModelResource):
tags = fields.ToManyField('django_app.api.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'

在posttags表中有一个已删除的标志,PostTags中删除的标记是否只能返回链接的结果?



我尝试过这个过滤器属性,但它似乎只关心链接表中的标志(即标签或帖子),而不是实际链接的表。

解决方案

您可以使用lambda包属性过滤字段,显示表名和字段名。

 code> tags = fields.ToManyField('django_app.api.TagsResource',attribute = lambda bundle:bundle.obj.tags.filter(tags__deleted = 0)) 


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.

解决方案

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

这篇关于Tastypie,过滤许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆