0
votes

What I want to accomplish is merge an unknown amount of querysets in the admin. I have a list with the authors a user can view and depending on the authors a user has in the list, he should be capable of seeing only their articles. What I have is:

def get_queryset(self, request):
    #getting all the lists and doing not important stuff
    return (qs.filter(author__name = list(list_of_authors)[0]) | qs.filter(author__name = list(list_of_authors)[len(list_of_authors)-1])).distinct()

This works if the user can view articles from two authors, however, for three it does not work. I tried using:

for index in list_of_authors:
    return qs.filter(author__name = list(list_of_authors)[index])

The Author class has a name = Charfield(max_length=50).

Sadly I got only the last queryset. Is it even possible to merge querysets when the amount is unknown, because after a decent amount of searching I did not end up finding anything.

1
Could you show the relevant models, especially the author field definition?Melvyn
I edited the question as there was a slight mistake.filtfilt

1 Answers

2
votes

You are looking for for __in lookup.

You name field is not a container and you're comparing it with a container. As you can tell, doing the hard work is not as easy, so Django has done it for you with that lookup.

The short version: qs.filter(author__name__in=list_of_authors)