0

Hello I am looking for the equivalent of SQL queries using Django for instance :

SELECT * FROM TABLE WHERE CONDITION

SELECT COLUMN_1 FROM TABLE WHERE CONDITION

UPDATE TABLE SET COLUMN_1 = 'values' WHERE condition

I saw different things on the internet so I would like to be sure that is why I ask your help.

Thank you !

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • 4
    What did you see on the internet? Didn't it work? – jarlh May 17 '19 at 08:45
  • Check [django docs on queries](https://docs.djangoproject.com/en/2.2/topics/db/queries/). They have details on how to use conditions, query only 1 column and update values. – Gasanov May 17 '19 at 08:47
  • Sorry but actually I don't understand the difference between `filter()` and `get()` – Bruce Collins May 17 '19 at 08:49
  • https://stackoverflow.com/questions/42899919/django-queryset-and-filter-vs-get/42900163 – Gasanov May 17 '19 at 08:53
  • Thank you I understoof but I don't find how can I do to select just one column ? like Select column_1 from ... – Bruce Collins May 17 '19 at 08:57
  • Use [values_list()](https://docs.djangoproject.com/en/dev/ref/models/querysets/#values-list) – Gasanov May 17 '19 at 09:00
  • @Gasanov: please do *not* use `value_list(..)` only in very rare cases, one should select only one column. You should see a table as a collection of model entities, not that much as "records". It also removes the behavior you defined on the models. Only in some cases, like some advanced `GROUP BY`s, this is a good idea. – Willem Van Onsem May 17 '19 at 09:14
  • @BruceCollins: it is usually better not to think in terms of SQL and then revert these to Django ORM calls. You should think the other way around: how can I retrieve entity objects, and filter/update/... these properly. To some extent, you can see SQL as an "implementation detail" of the ORM. – Willem Van Onsem May 17 '19 at 09:15
  • 1
    @WillemVanOnsem Yeah, pretty much better to just get model instances and loop over specific field. – Gasanov May 17 '19 at 09:19

1 Answers1

0

You can filter querys in django like this:

MyModel.objects.filter(description='Hello Wolrd!')

This returns QuerySet with all MyModel that the description equals Hello Wolrd!

Exemple of return:

<QuerySet [<MyModel: MyModel object (1)>,<MyModel: MyModel object (2)>,...]>

You can see that the return is Queryset with objects MyModel

If you want only some fields you can try it:

MyModel.objects.filter(description='Hello Wolrd!').values('name','id')

This returns only name and id.

Exemple of returns:

<QuerySet [{'name': 'Joao', 'id': 1}, {'name': 'Lucas', 'id': 2}, ...]>

This returns is like dict

If you can update value from model you need like this:

For one model:

m = MyModel.objects.get(id=1)
m.name = 'OlokoMeu'
m.save()

or

With filter objects

MyModel.objects.filter(description='Hello World!').update(name='Lorem')

for more informations : https://docs.djangoproject.com/en/2.2/topics/db/queries/ https://docs.djangoproject.com/pt-br/2.1/ref/models/querysets/#django.db.models.query.QuerySet.update

Lucas Resende
  • 582
  • 5
  • 14