'django dynamic content in query

I am trying to "DRY" my code and would like to create a function to make my query dynamic.
The code that I currently use is :

rightone = []
for item in taglist:    #taglist is a list of model instances, not relevant here
   content = content.filter(tags__title=item.title) #tags here is a M2M key : my problem, content is a query 
   rightone.append(content)
tagproofquery = rightone[-1]

and I would like to convert it to:

def uniquetogether(queryset,data, model):
                rightone = []
                for item in queryset:
                    content = data.filter(tags__title=item.title) #  <--- problem is here with tags
                    rightone.append(content)
                tagproofquery = rightone[-1]
                return tagproofquery
  

I have no idea how to replace my M2M "tags" as in tags__title=item.title with the "model" parameter of my function. I tried f strings but it failed miserably (of course). Is there a way to do this? Many thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source