Skip to content

Tag: django

A query with 3 tables in django

I have 3 classes like this: My question is: How can I make a query to get the devices used by the person “name”? Which needs to pass through the uses class. Answer Life will be easier if you use ManyToManyField, i.e. add this field to the Person model: Then, to get the list, you just need to get the

Django: Adding “NULLS LAST” to query

I would like to sort a model by using Postgresql’s “NULLS LAST” option. How could it be done? I tried something like MyModel.objects.all().extra(order_by=(‘-price’, ‘NULLS LAST’)) But I get “Cannot resolve keyword ‘NULLS LAST’ into field” Answer This functionality has been added to Django 1.11. Added the nulls_first and nulls_last parameters to Expression.asc() and desc() to control the ordering of

django: datediff sql queries?

I’m trying to do the equivalent of the following SQL in Django: SELECT * FROM applicant WHERE date_out – date_in >= 1 AND date_out – date_in <= 6 I can do this as a RAW sql query, but this is ...

Aggregating save()s in Django?

I’m using Django with an sqlite backend, and write performance is a problem. I may graduate to a “proper” db at some stage, but for the moment I’m stuck with sqlite. I think that my write performance problems are probably related to the fact that I’m creating a large number of rows, and presumably each time I save() one it’s

How to use “AND” in a Django filter?

How do I create an “AND” filter to retrieve objects in Django? e.g I would like to retrieve a row which has a combination of two words in a single field. For example the following SQL query does exactly that when I run it on mysql database: How do you accomplish this in Django using filters? Answer (update: this answer