B
B
Bjornie2017-10-03 13:02:42
Django
Bjornie, 2017-10-03 13:02:42

How to get repeats in queryset?

I'll give you an example right away. I have a list of ten id's (1,1,2,2,3,3,4,4,4,5). I need for each ID to check a certain checkbox (checked or unchecked) in the model and display the result.
What am I doing:

q = Model.objects.filter(id__in=[1,1,2,2,3,3,4,4,4,5])
no = q.filter(license=False),
yes = q.filter(license=True)

As a result, I get a queryset consisting of 5 objects, not ten. I understand that this is logical, but I need to get a result of 10 objects, regardless of the uniqueness of the identifiers. Please tell me how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2017-10-03
@Bjornie

Well then something like this:

q = [Model.objects.get(pk=x) for x in [1,1,2,2,3,3,4,4,4,5]]

no = list(filter(lambda x: x.license == False, q))
yes = list(filter(lambda x: x.license == True, q))

Not optimal, but solves your problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question