How to filter Django's CommaSeparatedIntegerField -
assume model
class foo(models.model):     bar = models.commaseparatedintegerfield('filter me!')   the content of bar like, e.g., 12,35,67,142.
i want query foos, have 42 in bar:
all_42_foos = foo.objects.filter(bar__contains="42")   which doesn't give correct result, since commaseparatedintegerfield inherits charfield , filter evaluation uses string content of field (matching above example 142, too).
how can hav filter, .split(",") on bar field before checks 42? don't want bar become manytomany, since it'd terrible overhead.
what like:
from django.db.models import q  all_42_foos = foo.objects.filter( q(bar__startswith='42,') | q(bar__endswith=',42') | q(bar__contains=',42,') | q(bar__exact='42') )   while it's bit of verbose query, think along lines way you're looking for. it's worth turning separate function along lines of
def all_x_foos(x):     return foo.objects.filter( q(bar__startswith=x+',') | q(bar__endswith=','+x) | q(bar__contains=',{0},'.format(x)) | q(bar__exact=x) )   out of curiosity, have checked actual performance of app w/ both many-to-many way, , pseudo-many-to-many method you're describing?
Comments
Post a Comment