django-q

django filter on many to many along with Q

拜拜、爱过 提交于 2019-12-11 09:41:24
问题 class ModelA: m2m1 = Foreignkey relatedname = 'm2ms' We can get m2m1 objects using: modela_instance.m2ms.all() Sample output: for x in ModelA.objects.all(): print x.m2ms.all().values_list('id', Flat=True) 1,2,3 1, [] 2 4,5 So, if I want fetch all instances whose m2ms has ids 1,2,3 (can have more too). Then I can do this: ModelA.objects.filter(m2ms__id=1).filter(m2ms__id=2).filter(m2ms__id=3) How can I do this with Q ? ModelA.objects.filter(Q(some_boolean=True, simple_field=5) | Q(some_boolean

Dynamically build complex queries with Q() in Django [closed]

假如想象 提交于 2019-12-10 17:12:14
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . First example: # ANDing Q objects q_object = Q() q_object.add(Q(), Q.AND) # ORing Q objects q_object = Q() q_object.add(Q(), Q.OR)

Always True Q object

廉价感情. 提交于 2019-12-10 02:31:56
问题 I want to create some part of Django ORM filter query dinamicly, now I can do: if some: Obj.filter( some_f1=some_v1, f1=v1, f2=v2, f3=v3, f4=v4, ... ) else: Obj.filter( f1=v1, f2=v2, f3=v3, f4=v4, ... ) I want something without code duplicate like this: Obj.filter( Q(some_f1=some_v1) if some else True, # what to use instead of True? f1=v1, f2=v2, f3=v3, f4=v4, ... ) 回答1: Here's a hacky way to get an always true Q object: always_true = ~Q(pk=None) This depends on the fact that the primary key

Always True Q object

别来无恙 提交于 2019-12-05 00:45:23
I want to create some part of Django ORM filter query dinamicly, now I can do: if some: Obj.filter( some_f1=some_v1, f1=v1, f2=v2, f3=v3, f4=v4, ... ) else: Obj.filter( f1=v1, f2=v2, f3=v3, f4=v4, ... ) I want something without code duplicate like this: Obj.filter( Q(some_f1=some_v1) if some else True, # what to use instead of True? f1=v1, f2=v2, f3=v3, f4=v4, ... ) Here's a hacky way to get an always true Q object: always_true = ~Q(pk=None) This depends on the fact that the primary key cannot be null. Try this; conditions = {'f1':f1,'f2':f2, 'f3':f3} if some: conditions['some_f1'] = some_v1

Perform a logical exclusive OR on a Django Q object

…衆ロ難τιáo~ 提交于 2019-12-04 21:24:27
问题 I would like to perform a logical exclusive OR (XOR) on django.db.models.Q objects, using operator module to limit the choices of a model field to a subset of foreignkey. I am doing this in Django 1.4.3 along with Python 2.7.2. I had something like this: import operator from django.conf import settings from django.db import models from django.db.models import Q from django.contrib.auth.models import User, Group def query_group_lkup(group_name): return Q(user__user__groups__name__exact=group

Django query filter combining AND and OR with Q objects don't return the expected results

限于喜欢 提交于 2019-12-03 10:45:51
问题 I try to combine AND and OR in a filter using Q objects. It looks like that the | behave like an AND. This is related to the previous annotate which is run in the same query and not as a subquery. What is the correct way to handle this with Django? models.py class Type(models.Model): name = models.CharField(_('name'), max_length=100) stock = models.BooleanField(_('in stock'), default=True) hide = models.BooleanField(_('hide'), default=False) deleted = models.BooleanField(_('deleted'), default

How to dynamically compose an OR query filter in Django?

淺唱寂寞╮ 提交于 2019-12-02 03:12:27
问题 From an example you can see a multiple OR query filter: Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3)) For example, this results in: [<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>] However, I want to create this query filter from a list. How to do that? e.g. [1, 2, 3] -> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3)) 回答1: You could chain your queries as follows: values = [1,2,3] # Turn list of values into list of Q objects queries = [Q(pk=value) for value in

Django SQL OR via filter() & Q(): Dynamic?

为君一笑 提交于 2019-11-30 09:40:46
I'm implementing a simple LIKE search on my Django website and what I currently use is the following code: from django.db.models import Q posts = Post.objects.filter(Q(title__icontains=query)|Q(content__icontains=query)) Where query is a string. This results in a LIKE SQL statement and works quite okay. Now I'd also like to split my search query into terms or words: words = query.split(' ') So words now contains a list of words, and I'd like to achieve an SQL statement similar to: SELECT ... FROM foo WHERE `title` ILIKE '%word1%' OR `title` ILIKE '%word2%' OR `content` ILIKE '%word1%' OR

django Building a queryset with Q objects

点点圈 提交于 2019-11-30 09:37:14
问题 I have a form that allows you to pick multiple project types to filter from. For instance, say you have the project types "Research", "Training", and "Evaluation". Basically what I'm looking to do is build a queryset using Q objects like: projects.filter(Q(type__type="Research") | Q(type__type="Training")) I'm just not sure how to build this without the filter() input being a string, which produces an error: querystring = "" for t in types: querystring += " | Q(type__type="+t+")" projects

Django SQL OR via filter() & Q(): Dynamic?

廉价感情. 提交于 2019-11-29 14:39:57
问题 I'm implementing a simple LIKE search on my Django website and what I currently use is the following code: from django.db.models import Q posts = Post.objects.filter(Q(title__icontains=query)|Q(content__icontains=query)) Where query is a string. This results in a LIKE SQL statement and works quite okay. Now I'd also like to split my search query into terms or words: words = query.split(' ') So words now contains a list of words, and I'd like to achieve an SQL statement similar to: SELECT ...