Questions tagged [django-queryset]

Django querysets are the primary abstraction for retrieving objects from Django's ORM system

Django querysets are the primary abstraction for retrieving objects from Django's ORM system using custom SQL queries. They abstract both the creation of queries as well as the assembly of the model objects that are returned.

See also

6784 questions
2
votes
1 answer

Django annotate on property field

I'm using Django 2.0 and Django REST Framework I have a model like below. class Contact(models.Model): first_name = models.CharField(max_length=100) class AmountGiven(models.Model): contact = models.ForeignKey(Contact,…
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
2
votes
2 answers

How to aggregate sum over multiple date ranges in query set?

Is it possible to annotate .sum()s over multiple date ranges in one QuerySet Ie, basically combining these, so each object has the sum for each date range. query_set_week = DailyReports.objects.filter( date__range=('2018-08-27', '2018-08-31'))…
Andrew
  • 12,172
  • 16
  • 46
  • 61
2
votes
1 answer

Filtering and grouping in Python Model Objects after querying the database

I have the following model: class Item(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name='item',on_delete=models.SET_NULL) email = models.BooleanField(blank=True, null=True) ..... I…
user3541631
  • 3,686
  • 8
  • 48
  • 115
2
votes
1 answer

Filter by user is not staff using Q in another model queryset

I have the following model: class APC(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name='apc', on_delete=models.SET_NULL) type= models.CharField(choices=TYPE_CHOICES, max_length=5,…
user3541631
  • 3,686
  • 8
  • 48
  • 115
2
votes
4 answers

Get next and previous object from QuerySet by object

I have and object and QuerySet which contains this object. I need to get next and previous object of this QuerySet. How can I do that? I could get next this way: next = False for o in QuerySet: if next: return o if o==object: …
Milano
  • 18,048
  • 37
  • 153
  • 353
2
votes
0 answers

How to merge two differently ordered queryset from same model together one after another in django

I have tried work_orders = WorkOrder.objects.all() active_wo = work_orders.filter(status=OPEN).order_by('-raised_on') completed_wo = work_orders.filter(status=COMPLETE).order_by('-completed_on') But when i merge these above querysets together…
Susaj S N
  • 960
  • 1
  • 10
  • 22
2
votes
2 answers

Get List of related objects - Django Queryset

How do I use django's queryset to get a list of users from the MyUser table where transaction_paid is False in my UserBankTransaction table? class UserBankTransaction(models.Model): user = models.ForeignKey(MyUser) plaid_transaction_id =…
2
votes
2 answers

Django QuerySet difference method not working

Im trying to get the difference between two QuerySets and it is important to me that the answer of this difference be a QuerySet. So, the natural solution is to use the difference method of the Django queryset. However, when trying to do that Im…
Artur Baruchi
  • 33
  • 1
  • 5
2
votes
1 answer

Possibly impossible Django question: sort admin field on Count of ForeignKey with time filter?

I have a list of Book objects in Django, and I want admin users to be able to sort them by the number of attached Transations in the past three years. I can already do this for the number of Transaction objects over all time, using annotate and a…
AP257
  • 89,519
  • 86
  • 202
  • 261
2
votes
1 answer

How to count values across fields in Django?

MODELS class ModelA(models.Model): name = models.CharField() class ModelB(models.Model): MY_CHOICES = ( ('X', 'X'), ('Y', 'Y'), ('Z', 'Z'), ) modela = models.ForeignKey(ModelA, on_delete=models.CASCADE) …
Karl
  • 733
  • 1
  • 14
  • 28
2
votes
1 answer

Django: query filter for group in which user is part

I am trying to filter a view related to the group to which the logged in user belongs to. Lets say we have a user who belongs to a group DOGS. I figured out how to filter for a specific that means known group name = DOGS. Models.py from…
kingbrain
  • 116
  • 12
2
votes
2 answers

Django optimize queries with for loops

Is it possible to optimize Django to not run so many queries on something like this for student in Student.objects.all(): for course in student.course_set.all(): for grade in course.grade_set.filter(student=student): # do…
Bufke
  • 3,195
  • 3
  • 28
  • 28
2
votes
1 answer

In Django using fiter() then get() on queryset?

Can I combine the use of filter() and get() on querysets to return an object in a django view? I have the following view; def my_view(request, city, store, item): item = Item.objects.filter(store__city=city, city=city).get(item=item) Items are all…
thesteve
  • 2,413
  • 6
  • 26
  • 28
2
votes
1 answer

Django ORM to retrieve month name from date time

I need to retrieve month name with sum total price of the corresponding month. Here is my order table sample My try is: month_wise_sales =…
shafik
  • 6,098
  • 5
  • 32
  • 50
2
votes
1 answer

Include wildcard (%) in Django 'contains' query

According to Django docs Entry.objects.get(headline__contains='Lennon') Roughly translates to this SQL: SELECT ... WHERE headline LIKE '%Lennon%'; But if I want to do somethng like this (removing a wildcard): SELECT ... WHERE headline LIKE…
O James
  • 189
  • 1
  • 12