[Answered ]-Is there any acceptable way to chop/recombine Django querysets without using the API?

1👍

It would probably be needlessly complicated as well as bad practice to try and “re-transform” your list back into a QuerySet, the best thing to do is to use cleverer QuerySet filtering.

You should use the queryset filter by regex syntax to get the functionality you need. Documentation here.

For instance the equivalent to ismale() using regex would be something like…

People.objects.filter(myflags__regex=r'.M.') # <-- For matching something like 'MM5'
# Please note I haven't tested this regex expression, but the principal is sound

Also, while I’m admittedly not a database guru, I’m fairly certain using this sort of “flags” in a charfield is a rather inefficient way of doing things.

1👍

If you must convert a list back into a queryset, then the idiom I use is:

People.objects.filter(pk__in=[x.pk for x in list_of_objects])

Obviously, this hits the database again. But if you really need it.

Leave a comment