1.

Explain Q objects in Django ORM?

Answer»

Q objects are used to WRITE complex queries, as in filter() FUNCTIONS just `AND` the conditions while if you WANT to `OR` the conditions you can use Q objects. Let’s see an example:

from django.db import modelsfrom django.db.models import Q>> objects = Models.objects.get( Q(tag__startswith='Human'), Q(category=’Eyes’) | Q(category=’Nose’))```Query ExecutedSELECT * FROM MODEL WHERE tag LIKE ‘Human%’ AND (category=’Eyes’ OR category=’Nose’)```


Discussion

No Comment Found