开发者

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

开发者 https://www.devze.com 2023-02-18 02:02 出处:网络
I was to forced to use a models.CharField to store some additional flags in one of my models. So I\'m abusing each letter of the field as a flag. As an example, \'MM5\' would mean \"man, married, a开发

I was to forced to use a models.CharField to store some additional flags in one of my models. So I'm abusing each letter of the field as a flag. As an example, 'MM5' would mean "man, married, a开发者_开发问答ge above 50" and 'FS2' "female, single, age above 20".

I'm using methods to query/access these flags. Of course I cannot use these methods with the queryset API. I'm using list comprehensions calling the methods to filter an initial queryset and transform them into a plain list, which is good enough for most template feeding:

people = People.objects.filter(name__startswith='J')  
people_i_want = [p for p in people if p.myflags_ismale() and p.myflags_isolderthan(30)]

So, is there any ok'ish way to retransform these lists back into a queryset? Or to chop/filter a queryset based on the output of my methods without transforming it to a normal list in the first place?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消