Django queryset来匹配所有相关对象 [英] Django queryset to match all related objects
问题描述
现在,我有一个husk_segments的列表,我想知道所有这些都被抓住了通过一个特定的燕子。
我可以使用swallow.objects.filter(coconututs_carried__husk_sements__in = husk_segment_list)来显示这个燕子已经在列表中至少抓住了一个外壳段。现在,我怎么能表明燕子所携带的每个外壳都在这个列表中?
我可以使用swallow.objects.filter(coconututs_carried__husk_sements__in =
husk_segment_list)来显示这个燕子已经在列表中至少抓住了一个
的外壳段。
<没有,这是错误的,这给你一个从列表。
如果我明白了,我们正在谈论检查特定的燕子。
所以,从你的描述我猜您的模型看起来像这样:
类Swallow(models.Model):
name = models.CharField(max_length = 100)
class Coconut(models.Model):
swallow = models.ForeignKey(Swallow,related_name ='coconututs_carried')
class HuskSegment(models.M odel):
coconut = models.ForeignKey(Coconut,related_name ='husk_segments')
如果您已经拥有了外壳段列表,则需要重新检查燕子片段,因此您无需在查询中解决它。获取燕子的细分,并检查它是否是您的外壳细分列表的超集。
所以我们有:
#husk_segment_list = [< object:HuskSegment>,< object:HuskSegment>< object:HuskSegment> ...]
husk_segments_set = set((husk.pk for husk in husk_segment_list))
whitey = Swallow.object.get(name ='Neochelidon tibialis')
wh_segments_set = set((value [0] for HuskSegment.objects.filter (coconut__in = whitey.coconuts_carried.all())。values_list('id')))
whitey_has_carried_all = wh_segments_set.issuperset(husk_segments_set)
Let's say I have a ForeignKey from Coconut to Swallow (ie, a swallow has carried many coconuts, but each coconut has been carried by only one swallow). Now let's say that I have a ForeignKey from husk_segment to Coconut.
Now, I have a list of husk_segments and I want to find out if ALL of these have been carried gripped by a particular swallow.
I can have swallow.objects.filter(coconuts_carried__husk_sements__in = husk_segment_list) to show that this swallow has gripped at least one husk segment in the list. Now, how can I show that every husk segment that the swallow has ever carried are in this list?
解决方案I can have swallow.objects.filter(coconuts_carried__husk_sements__in = husk_segment_list) to show that this swallow has gripped at least one husk segment in the list.
No, this is wrong, this gives you a list of swallows which have carried at least one husk segment from *husk_segment_list*.
If I've understood right, we are talking about checking for a specific swallow.
So, from your description I guess your models look something like this:
class Swallow(models.Model): name = models.CharField(max_length=100) class Coconut(models.Model): swallow = models.ForeignKey(Swallow, related_name='coconuts_carried') class HuskSegment(models.Model): coconut = models.ForeignKey(Coconut, related_name='husk_segments')
If you already have the husk segment list you need to check againts the swallows segments, there's no reason you need to resolve it in a query. Get the swallows' segments and check if it's a superset of your husk segment list.
So we have:
#husk_segment_list = [<object: HuskSegment>, <object: HuskSegment>, <object: HuskSegment>...] husk_segments_set = set((husk.pk for husk in husk_segment_list)) whitey = Swallow.object.get(name='Neochelidon tibialis') wh_segments_set = set((value[0] for value in HuskSegment.objects.filter(coconut__in=whitey.coconuts_carried.all()).values_list('id'))) whitey_has_carried_all = wh_segments_set.issuperset(husk_segments_set)
这篇关于Django queryset来匹配所有相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!