django-q

Django Aggregate Query Include Zero Count

末鹿安然 提交于 2021-02-11 16:11:27
问题 In my Django application, I'm trying to get a Count of all Student submitted Paper s, including students who have submitted NO papers (represented as count=0). models.py class Student(models.Model): idstudent = models.AutoField(primary_key=True) student_name = models.CharField(max_length=250, null=False, blank=False, verbose_name='Student Name') class Paper(models.Model): idpaper = models.AutoField(primary_key=True) student = models.ForeignKey(Student, on_delete=models.PROTECT, null=False,

Django. Q objects dynamicaly generate

折月煮酒 提交于 2019-12-23 04:59:15
问题 There is a filter for the model fields queryset = queryset.filter( Q(title__icontains=search_text) | Q(description__icontains=search_text) | Q(name_icontains=search_text) ) How i can do generate block according to circumstances Q(title__icontains=search_text) | Q(description__icontains=search_text)| Q(name_icontains=search_text) For example, in one case it is necessary that the filter be such Q(description__icontains=search_text) | Q(name_icontains=search_text) or Q(title__icontains=search

Trying to reduce Django Q objects with operator.or_ seems to result in reduction with 'AND'

不羁岁月 提交于 2019-12-23 03:00:14
问题 I am working on an application in Python/Django. I am trying to make a filter by reducing a list of Q objects with Python's operator.or_ function. Unfortunately it results in a list that is combined with an AND rather than operator.or_ . The problem occurs in the following code: print 'operator.or_', operator.or_ filter = reduce(operator.or_, q_objects[key]) print key, '->', filter The statement print 'operator.or_', operator.or_ results in operator.or_ <built-in function or_> so that seems

Filtering with a Q object on an annotated QuerySet

混江龙づ霸主 提交于 2019-12-18 06:48:55
问题 A mock testcase: def testCount(self): qs = Test.objects.all() qs = qs.annotate(a_count=Count('a_items'), b_count=Count('b_items')) for item in qs: print 'a_count: %d, b_count: %d' % (item.a_count, item.b_count) qs1 = qs.filter(Q(a_count__gt=0)) self.assertEquals(qs1.count(), 1) qs2 = qs.filter(Q(a_count__gt=0) | Q(b_count__gt=0)) self.assertEquals(qs2.count(), 1) Output: a_count: 1, b_count: 0 a_count: 0, b_count: 0 ... FAIL: testCount self.assertEquals(qs2.count(), 1) AssertionError: 0 != 1

How to dynamically compose an OR query filter in Django?

被刻印的时光 ゝ 提交于 2019-12-17 01:36:22
问题 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 Q Queries & on the same field?

孤者浪人 提交于 2019-12-13 16:15:04
问题 So here are my models: class Event(models.Model): user = models.ForeignKey(User, blank=True, null=True, db_index=True) name = models.CharField(max_length = 200, db_index=True) platform = models.CharField(choices = (("ios", "ios"), ("android", "android")), max_length=50) class User(AbstractUser): email = models.CharField(max_length=50, null=False, blank=False, unique=True) Event is like an analytics event, so it's very possible that I could have multiple events for one user, some with platform

Django, having Q object filter by function in model

我的未来我决定 提交于 2019-12-13 13:38:10
问题 Inside my Profile model I have the following function: It serves to return the fullname of the user (or a alternative if some data is missing). def full_name(self): first_name = self.user.first_name.strip() if first_name and self.user.last_name: if not self.middle_name: full_name = u'%s %s' % (first_name, self.user.last_name) else: full_name = u'%s %s %s' % (first_name, self.middle_name, self.user.last_name) return full_name.strip() elif first_name: return first_name return self.user.username

A Django ORM query using a mix of filter() and Q objects

大兔子大兔子 提交于 2019-12-12 11:50:04
问题 I'm looking to create a slightly more complex query that is written fairly easily using raw SQL. Here's an example of the query in raw: SELECT my,fields FROM sales WHERE is_paid = False OR status = 'toship' AND otherfield = 'FOO' AND anotherfield = 'BAR' This is simple, it generates all the results that are is_paid = False and then a second result set for my AND matches. Now I know about Q objects, I know about filtering but I can't seem to wrap my mind around how to achieve this in the

Need to annotate Django querySet based on which Q object was found

我的梦境 提交于 2019-12-12 01:52:51
问题 So I have a query with a few Q objects that are OR-ed together (to achieve a UNION), and I want to annotate each result with which Q object was a match. This is so when I go to display my query results, I can highlight which search term(s) were hits on each result. Here's the code that produces the resulting querySet: Gene.objects.filter(Q(EC__EC='3.2.1.4')|Q(Protein_Family__name__in=famList)|Q(Pfam__Pfam__in=pfams),Protein_length__gte=100, Distance_From_Contig_Upstream__gte=10, Distance_From

ADDing Django's Q() objects and get two exclusive JOIN ONs

守給你的承諾、 提交于 2019-12-11 17:33:37
问题 So this is the scenario: class Person(models.Model): ... class Aktion(models.Model): ... class Aktionsteilnahme(models.Model): person = models.ForeignKey(Person) aktion = models.ForeignKey(Aktion) The problem now is, that I'm dynamically constructing rather complex queries based on Q() -objects. They end up like this: Person.objects.filter( Q((Q()|Q(aktionsteilnahme__aktion=302))& (Q()|Q(aktionsteilnahme__aktion=547))) ) which can (and automatically will) be reduced to: Person.objects.filter(