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
Tag: django
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. https://docs.djangoproject.com/en/dev/releases/1.11/ Added the nulls_first and nulls_last parameters to Expression.asc() and desc() to control the ordering of
Filtering Django Query by the Record with the Maximum Column Value
Is there an easy way to filter a Django query based on which record has a max/min value in a column? I’m essentially asking these questions, but in the specific context of Django’s ORM. e.g. Say I have a model designed to store the historical values of everyone’s phone numbers. with the records: Now say I wanted to find everyone’s
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 ...
How come queries aren’t being added to Django’s db.connection.queries in tests?
I’m trying to capture the queries which my code submits to the database by examining the contents of django.db.connection.queries. For some reason though, after all the automatically produced setup queries are logged, no further queries are logged from my own code. The following test case demonstrates the behavior. And here are the results of running it: Would anyone be able
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
Keep history in django to draw graphs
I am looking for the best way to have an history of my models (interger & float fields) in Django. I read Keeping a history of data changes in database and it seems that triggers are the best …
How can I see the raw SQL queries Django is running?
Is there a way to show the SQL that Django is running while performing a query?
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