4👍
slug = ["snakes", "snake-s" ] # in the real world this is generated from directory structure on disk
# build the query
query = MyModel.objects
if hasattr(slug, "__iter__"):
q_list = []
for s in slug:
q_list.append(Q(slug=s))
query = query.filter(reduce(operator.or_, q_list))
else:
query = query.filter(slug=slug)
q_list = []
create a list of Q clausesreduce(operator.or_, q_list)
implode the list with or operators
read this: http://www.michelepasin.org/techblog/2010/07/20/the-power-of-djangos-q-objects/
@MostafaR – sure we could crush my entire codeblock down to one line if we wanted (see below). Its not very readable anymore at that level though. saying code isn’t “Pythonic” just because it hadn’t become reduced and obsfucated is silly. Readable code is king IMHO. Its also important to keep in mind the purpose of my answer was to show the reduce
by an operator technique. The rest of my answer was fluff to show that technique in context to the original question.
result = MyModel.objects.filter(reduce(operator.or_, [Q(slug=s) for s in slug])) if hasattr(slug, "__iter__") else MyModel.objects.filter(slug=slug)
1👍
result = MyModel.objects.filter(slug__in=slug).all() if isinstance(slug, list) else MyModel.objects.filter(slug=slug).all()
- [Django]-Importing celery in project fails, works(sort of) in manage.py shell
- [Django]-How to read contents of zip file in memory on a file upload in python?
0👍
I believe in this case you should use django’s __in
field lookup like this:
slugs = [ "snakes", "snake-s" ]
objects = MyModel.objects.filter(slug__in=slugs)
- [Django]-Stop abstract django.test TestCase class from running test also
- [Django]-Django + Celery with AWS SQS – Running on localhost instead of AWS and not getting messages
-1👍
The code that you posted will not work in many ways (but I am not sure if it should be more pseudocode?), but from what I understand, this might help:
MyModel.objects.filter(slug__in=slug)
should do the job.
- [Django]-Django aggregate by field with no related name
- [Django]-Many-To-One Relation Query in Django
- [Django]-Django Queryset Iterator for Ordered queryset
- [Django]-Getting first image from html using Python/Django
- [Django]-Why Django does not check email integrity when creating a new user?