Search code examples
pythondjangomany-to-manydjango-queryset

Filtering a model's ManyToMany field with __in=[somelist] produces zero results when it should produce several


I haven't found a solution online to this--I can't be the first person attempting this.

Here's my model structure:

class TopModel(models.Model):
    type = models.CharField()

class MiddleModel(models.Model):
    parent = models.ForeignKey(TopModel)

class LowerModel(model.Model):
    middleParent = models.ForeignKey(MiddleModel)
    topParent = models.ForeignKey(TopModel, related_name="ref_to_top_parent")
    topmodel_refs = models.ManyToMany(TopModel, related_name="ref_to_other_tops")

I need to filter by some weird querying. I use the MiddleModel to filter the Top Model twice--you'll see.

So I'm making 2 query sets:

currentMidMod = MiddleModel.objects.get(pk=1)
firstQuerySet = currentMidMod.topmodel_set.all()

secondMidMod = MiddleModel.objects.get(pk=2)
secondQuerySet = secondMidMod.topmodel_set.all()

Then I filter the 1st:

firstQuerySet = firstQuerySet.filter(type__contains="somevalue")

Then I filter the 2nd:

secondQuerySet = secondQuerySet.filter(type__contains="test") #produces a count() of 10
#Grab a valueset of the pks
compareList = list(secondQuerySet.values_list('pk', flat=True))

Then here is where the issue occurs:

newQuerySet = firstQuerySet.filter(ref_to_top_parent__topmodel_refs__in=compareList)

newQuerySet.count() #equals 0 when I know it should equal 5

So I'm trying to filter my first queryset by all of its LowerModel reverse foreignkeys by those LowerModels ManyToMany list of keys back to TopModels by comparing it with a list of pk values in the values list I create

It never finds matching 'pks' even though I can manually look through phpAdmin and find the matches myself

I'm dying. Please help!

EDIT: when trying to query a single PK rather than a list--it works fine e.g.

newQuerySet = firstQuerySet.filter(ref_to_top_parent__topmodel_refs__pk=74956)

When trying to compare that ManyToMany field to a list() of values(that includes '74956') it doesn't find any matches--it should at least be finding '74956' in the values_list I imagine.


Solution

  • As discussed in the comments on the original question, the entries were not being created in the intermediary table for the ManyToMany relationship. I just want internet points! :)