Questions tagged [django-orm]

Django's ORM system, comprising its queryset and model systems.

Django's ORM system to fetch, update and store data from the models into the database and vice versa. The tag comprises its queryset and model systems.

See also:

3993 questions
455
votes
18 answers

Convert Django Model object to dict with all of the fields intact

How does one convert a django Model object to a dict with all of its fields? All ideally includes foreign keys and fields with editable=False. Let me elaborate. Let's say I have a django model like the following: from django.db import…
Zags
  • 37,389
  • 14
  • 105
  • 140
426
votes
6 answers

How to perform OR condition in django queryset?

I want to write a Django query equivalent to this SQL query: SELECT * from user where income >= 5000 or income is NULL. How to construct the Django queryset filter? User.objects.filter(income__gte=5000, income=0) This doesn't work, because it ANDs…
Elisa
  • 6,865
  • 11
  • 41
  • 56
300
votes
1 answer

How to query Case-insensitive data in Django ORM?

How can I query/filter in Django and ignore the cases of my query-string? I've got something like and like to ignore the case of my_parameter: MyClass.objects.filter(name=my_parameter)
Ron
  • 22,128
  • 31
  • 108
  • 206
224
votes
4 answers

Django self-referential foreign key

I'm kind of new to webapps and database stuff in general so this might be a dumb question. I want to make a model ("CategoryModel") with a field that points to the primary id of another instance of the model (its parent). class…
sfendell
  • 5,415
  • 8
  • 25
  • 26
194
votes
8 answers

How to view corresponding SQL query of the Django ORM's queryset?

Is there a way I can print the query the Django ORM is generating? Say I execute the following statement: Model.objects.filter(name='test') How do I get to see the generated SQL query?
DjangoNewbe
175
votes
6 answers

How to rename items in values() in Django?

I want to do pretty much the same like in this ticket at djangoproject.com, but with some additonal formatting. From this query >>> MyModel.objects.values('cryptic_value_name') [{'cryptic_value_name': 1}, {'cryptic_value_name': 2}] I want to get…
Martin
  • 4,170
  • 6
  • 30
  • 47
174
votes
6 answers

How to create an object for a Django model with a many to many field?

My model: class Sample(models.Model): users = models.ManyToManyField(User) I want to save both user1 and user2 in that model: user1 = User.objects.get(pk=1) user2 = User.objects.get(pk=2) sample_object = Sample(users=user1,…
Sai Krishna
  • 7,647
  • 6
  • 29
  • 40
170
votes
6 answers

What is the SQL ''LIKE" equivalent on Django ORM queries?

What is the equivalent of the following SQL statement in Django? SELECT * FROM table_name WHERE string LIKE pattern; I tried this: result = table.objects.filter( pattern in string ) but it didn't work. How can I implement it?
Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
165
votes
6 answers

Django database query: How to get object by id?

Django automatically creates an id field as primary key. Now I need to get the object by this id. object = Class.objects.filter() How to write this filter?
user469652
  • 48,855
  • 59
  • 128
  • 165
163
votes
5 answers

Select distinct values from a table field

I'm struggling getting my head around the Django's ORM. What I want to do is get a list of distinct values within a field on my table .... the equivalent of one of the following: SELECT DISTINCT myfieldname FROM mytable (or alternatively) SELECT…
alj
  • 2,839
  • 5
  • 27
  • 37
162
votes
6 answers

Chaining multiple filter() in Django, is this a bug?

I always assumed that chaining multiple filter() calls in Django was always the same as collecting them in a single call. # Equivalent Model.objects.filter(foo=1).filter(bar=2) Model.objects.filter(foo=1,bar=2) but I have run across a complicated…
gerdemb
  • 11,275
  • 17
  • 65
  • 73
140
votes
6 answers

Django select only rows with duplicate field values

suppose we have a model in django defined as follows: class Literal: name = models.CharField(...) ... Name field is not unique, and thus can have duplicate values. I need to accomplish the following task: Select all rows from the model that…
dragoon
  • 5,601
  • 5
  • 37
  • 55
134
votes
4 answers

Select DISTINCT individual columns in django?

I'm curious if there's any way to do a query in Django that's not a "SELECT * FROM..." underneath. I'm trying to do a "SELECT DISTINCT columnName FROM ..." instead. Specifically I have a model that looks like: class ProductOrder(models.Model): …
jamida
  • 2,869
  • 5
  • 25
  • 22
133
votes
10 answers

Why is iterating through a large Django QuerySet consuming massive amounts of memory?

The table in question contains roughly ten million rows. for event in Event.objects.all(): print event This causes memory usage to increase steadily to 4 GB or so, at which point the rows print rapidly. The lengthy delay before the first row…
davidchambers
  • 23,918
  • 16
  • 76
  • 105
111
votes
4 answers

Django filter many-to-many with contains

I am trying to filter a bunch of objects through a many-to-many relation. Because the trigger_roles field may contain multiple entries I tried the contains filter. But as that is designed to be used with strings I'm pretty much helpless how i should…
Grave_Jumper
  • 1,224
  • 2
  • 8
  • 11
1
2 3
99 100