Django queryset to match all related objects -
let's have foreignkey coconut swallow (ie, swallow has carried many coconuts, each coconut has been carried 1 swallow). let's have foreignkey husk_segment coconut.
now, have list of husk_segments , want find out if of these have been carried gripped particular swallow.
i can have swallow.objects.filter(coconuts_carried__husk_sements__in = husk_segment_list) show swallow has gripped @ least 1 husk segment in list. now, how can show every husk segment swallow has ever carried in list?
i can have swallow.objects.filter(coconuts_carried__husk_sements__in = husk_segment_list) show swallow has gripped @ least 1 husk segment in list.
no, wrong, gives list of swallows have carried @ least 1 husk segment *husk_segment_list*.
if i've understood right, talking checking specific swallow.
so, description guess models 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 have husk segment list need check againts swallows segments, there's no reason need resolve in query. swallows' segments , check if it's superset of husk segment list.
so have:
#husk_segment_list = [<object: husksegment>, <object: husksegment>, <object: husksegment>...] husk_segments_set = set((husk.pk husk in husk_segment_list)) whitey = swallow.object.get(name='neochelidon tibialis') wh_segments_set = set((value[0] 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)
Comments
Post a Comment