- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这与 Django filter queryset __in for *every* item in list 有点不同和 Django filter queryset __in for *every* item in list (2.0)给定以下模型:
class Product(BaseModel):
''' whatever '''
class Customer(BaseModel):
blacklist = models.ManyToManyField(Product, blank=True)
class Advisory(BaseModel):
product_names = models.ManyToManyField(Product)
客户维护其不感兴趣的产品列表。我如何获取给定客户的建议列表?
假设我的数据库中有这些:
# Advisory 1 (should be included as Product 1 isn't in the customers blacklist)
# product_names
# Product 1
# Product 2
# Product 3
# Advisory 2 (should be excluded)
# product_names
# Product 2
# Product 3
# Customer 1
# blacklist
# Product 2
# Product 3
# Product 4
# Product 5
如果我使用这样的查询集:
queryset = Advisory.objects.all()
blacklist = Customer.blacklist.all()
queryset = queryset.exclude(product_names__in=blacklist).distinct()
它将排除公告 1 和公告 2,因为产品 2 和 3 存在于客户黑名单中
最佳答案
我通过两步找到了问题的解决方案:
# Get the queryset with all advisories
queryset = Advisory.objects.all()
# Find the products the customer doesn't care about
blacklist = Customer.objects.get(pk=customer_id).blacklist.all()
# Build a whitelist of products excluding the blacklist from the customer
whitelist = Product.objects.all().exclude(id__in=blacklist)
# And filter the queryset with the new whitelist
queryset = queryset.filter(product_names__in=whitelist).distinct()
关于python - Django 排除列表中*每个*项目的查询集 __in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50463123/
我有特定顺序的 ID >>> album_ids = [24, 15, 25, 19, 11, 26, 27, 28] >>> albums = Album.objects.filter( id__i
嗨,我想在没有任何连接的情况下在两个模型之间进行查询 我想检查 ModelA 中是否有名字模型在 User class ModelA(models.Model): name = models.
我在 Django 中设置了一个复杂的数据库模型,我必须根据过滤器数据进行一些计算。我有一个 Test对象,一个 TestAttempt对象,和一个 UserProfile对象(带有返回测试的外键和返
所以我想在给定的一些字段中找到任何类型的匹配,例如,这就是我想做的: possible_merchants = ["amazon", "web", "services"] # Possible nam
所以这就是我想要做的。 我有一个包含年份的列表,例如 years = [2002, 2003, 2004]我有一个 SomethingModel与 DateField 我想做一个查询,返回属于那一年的
之间有什么区别 queryset.filter(Q(foo='bar') | Q(foo='baz')) 和 queryset.filter(foo__in=['bar', 'baz']) 我发现有时
a = M.objects.filter(f__in=[None, 1]) a.query.__str__() u'SELECT * FROM "app_m" WHERE "app_m"."f" IN
在关键字中的变量之前使用这些关键字的含义是什么 功能参数? __in __out __in_opt 最佳答案 这些是Microsoft SAL注释中的一些较旧的装饰宏(the newer ones现在
我有这个查询 query = 'select * from products where (productnr, supplier_id) in (%s)' % product_list 其中,pro
我有一个 Story 模型,其中包括通过 django 标记应用程序的 TagField。我正在获取特定标签的故事列表。从功能上讲,我的代码工作正常,但我遇到了一种我不太理解的行为。 以下代码正确地为
我在 msdn 的套接字定义中看到如下内容: SOCKET WSAAPI socket( __in int af, __in int type, __in int protocol
这与 Django filter queryset __in for *every* item in list 有点不同和 Django filter queryset __in for *every
我正在做标签搜索功能,用户可以观察到很多标签,我把所有的都放在一个元组中,现在我想从列表中找到至少包含一个标签的所有文本。 符号:text__contains__in=('asd','dsa') 我唯
假设我是部落酋长,俯瞰部落事件。我有 3 个型号: class Hunter: tribes = models.ManyToManyField(Tribe) info class Miss
我有一个相当复杂的数据库,它由一个模型 Bar 组成,它可能有几个 Spam,通过 ForeignKey 关系实现(每个 Spam 知道它属于哪个 Bar)。另一个模型 Foo 恰好有两个 Bars。
我今天阅读了 VS2010 中的 sal.h。 我有点惊讶。 // This section contains the deprecated annotations |------------|---
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the purpose of __in __out __in_opt __allowed()
假设我有以下型号 class Offer(models.Model): skills_required = models.ManyToManyField(Skill, blank=True)
在 Django 中使用“__in”过滤查询集的首选方法是什么? providers = Provider.objects.filter(age__gt=10) consumers = Consume
正在关注 this question , 想知道写法有没有区别 .filter(league_pk__in=[1,2,3]) 和 .filter(league=[1,2,3]) 我都试过了,它们似乎在
我是一名优秀的程序员,十分优秀!