简体   繁体   English

使用用户 manyTomany 过滤对象 manyTomany

[英]Filter objects manyTomany with users manyTomany

I want to filter the model Foo by its manyTomany field bar with users bar .我想通过其 manyTomany 字段bar和 users bar过滤 model Foo

Models楷模

class User(models.Model):
  bar = models.ManyToManyField("Bar", verbose_name=_("Bar"), blank=True)

class Foo(models.Model):
  bar = models.ManyToManyField("Bar", verbose_name=_("Bar"), blank=True)

class Bar(models.Model):
  fubar = models.CharField()

with this有了这个

user = User.objects.get(id=user_id)用户 = User.objects.get(id=user_id)

I want to gett all Foo's that have the same Bar's that the User has.我想得到所有具有与用户相同的 Bar 的 Foo。 I would like this to work:我希望这个工作:

bar = Foo.objects.filter(foo=user.foo) bar = Foo.objects.filter(foo=user.foo)

but it doesn't work.但它不起作用。

foos = Foo.objects.filter(bar__in=user.bar.all())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM