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
3 answers

How can I do join in django?

I'm working with django, expecially with QuerySet right now and I have a very big problem. so, this is my code: resources_rights = Resources_rights.objects.filter( group_id__in = groups ) resources = Resources.objects.filter( id__in =…
ChiaraM
  • 65
  • 1
  • 10
2
votes
2 answers

Django get ID of Max after GROUP BY

I have two models: Forum: pk (ID) Thread: pk (ID), last_activity (datetime), forum (foreign key) I want to retrieve all the latest threads, group them by forum and in addition to that get at least the ID of each latest thread. Here's what I…
user2438069
2
votes
1 answer

time difference with F experession: error

When I try to calculate a time difference between two fields using F expression it results in TypeError. I can't understand what's the problem, especially because there are other examples where F expressions were used to do the same (here: Timedelta…
Philipp Chapkovski
  • 1,949
  • 3
  • 22
  • 43
2
votes
1 answer

Django somehow cannot determine simple annotate function

I'm trying to create an annotation on a queryset class that simply adds a boolean that is the result of some standard queries. CustomQueryset(models.QuerySet): """ An extension of the traditional queryset to support filtering on…
Stefan Collier
  • 4,314
  • 2
  • 23
  • 33
2
votes
3 answers

How to access the a reverse relation of a reverse relation django

I am determined to try get all the other users that replied to the same comment as a given reply. I have come up with the following python, however I am concerned this will do more than one DB call. other_repliers = [other_reply.user for other_reply…
Stefan Collier
  • 4,314
  • 2
  • 23
  • 33
2
votes
2 answers

Django filter based on custom function

I have a table AvailableDates with a column date that stores date information. I want to filter the date after performing some operation on it which is defined by convert_date_to_type function, that takes parameter input_variable provided by…
2
votes
2 answers

Django ORM calculations between records

Is it possible to perform calculations between records in a Django query? I know how to perform calculations across records (e.g. data_a + data_b). Is there way to perform say the percent change between data_a row 0 and row 4 (i.e. 09-30-17 and…
ac2001
  • 726
  • 1
  • 6
  • 19
2
votes
1 answer

Django queryset filter max values for list

I'm wondering if there is any way to pass a list to a django queryset and limit to the max/min/latest/oldest value for each item in the list. Basically something like this: serial_list = [2231, 2232, 2233] data_queryset =…
Jon
  • 432
  • 2
  • 6
  • 20
2
votes
1 answer

Django QuerySet.union() -or- QuerySet.raw() to Achieve Case-Insensitive Mutli-Search

Given a URL like this: http://..../search/?foo=a&foo=B&bar=whatever In my View, I want to filter the QuerySet for CASE-INSENSITIVE matches of "foo". There's a handy "in" operator that I could use on my list of "foo", but it doesn't support…
Lance E.T. Compte
  • 932
  • 1
  • 11
  • 33
2
votes
1 answer

How do I filter a query set based on child attributes after following a ForeignKey backwards?

I'm new to Python/Django, so any help is much appreciated! I am trying to find out a winner based on how many times score_1 > score_2 in all the child objects. I have these two Models: class Parent(models.Model): winner =…
2
votes
2 answers

How to pass RawQuerySet result as a JSONResponse in DJango?

I have two models like this: class McqQuestion(models.Model): mcq_question_id = models.IntegerField() test_id = models.ForeignKey('exam.Test') mcq_right_answer = models.IntegerField() class UserMcqAnswer(models.Model): user =…
Nafi Pantha
  • 169
  • 1
  • 3
  • 16
2
votes
1 answer

Django Rest Framework: advanced queryset filtering based on another model

I am using Django Rest Framework as a backend for an app. I have a User that has one Wallet. Then I have Item. If a User wants an Item it creates an instance in his/her Wallet called WalletItem. All works well. Now I want to limit the number of…
2
votes
1 answer

Boolean annotation resulting in duplicates?

I am trying to implement a basic 'favourites' system based on a foreign key table. Let's say I have the following simple models: class Item(models.Model) id = models.IntegerField() class User(models.Model) id = models.IntegerField() class…
Eric
  • 601
  • 7
  • 22
2
votes
2 answers

Using Tag model to create ManytoMany relationship

I am trying to fetch data where the column value "tag" belongs to list from the table "UserBookmark". UserBookmark.objects.filter(tag__in = ['Java','Android']) but this returns QuerySet[](null set) whereas I do have data matching this query in…
Dhruv Pandey
  • 482
  • 6
  • 18
2
votes
4 answers

Elegant way of fetching multiple objects in custom order

What's an elegant way for fetching multiple objects in some custom order from a DB in django? For example, suppose you have a few products, each with its name, and you want to fetch three of them to display in a row on your website page, in some…
Danra
  • 9,546
  • 5
  • 59
  • 117
1 2 3
99
100