Through this article, MarsDevs will help you understand the different ways of querying in Django, as, at the start, it might look and feel quite complicated.
Note: Please read before copying the code.
Whenever we say model, it means table. And the object here means a row in that table.
A Django query is an expression that we can use to write/ retrieve/ update/ delete data from the database.
This is not a question that needs any answer. If you are here, that means you know that when a user interacts with a web application, it requires database operations. Thus we need Django queries to interact with the database.
For us to demonstrate how to use Django queries, let's make some assumptions.
We'll use two models majorly and add more if required.
User stores user-related data, and UserEducationDetails stores user’s education details.
Assumption: We already have some data in both models.
All the operations performed will be in views.py, so one needs to import both models.
Fetch all the data from User Model
queryset = User.objects.all()
This will return a list of objects (called as queryset)
How to access the data from the above queryset?
All you have to do is iterate through.
Fetch all the data from UserEducationDetails Model
Now we want to find out whose id is 1
queryset = User.objects.filter(id=1)
And you will get a queryset with the object whose id is one if present. Otherwise, the queryset will be empty, but no errors will be thrown.
Alternatively, one can use .get, but this will throw an error if there is no object or multiple objects are found.
obj = User.objects.get(id=1)
How to access the data from the above object?
Now let’s try some complicated queries.
queryset = UserEducationDetails.objects.filter(id=1).select_related(“user”)
All you have to do is add select_related(“user”), where the user is the field name in the UserEducationDetails model.
How about you also want to filter based on related model’s data
Here we are filtering where UserEducationDetails id=1 and first_name of the user is “kira”
queryset = User.objects.filter(id=1).prefetch_related(“education_details”)
This will return data from both the models.
If you noticed, we have used prefetch_related(“education_details”) where education_details is used in UserEducationDetails model
related_name=”education_details” and this is how we can get data from both the models without querying both of them separately.
You can assume the above query to be like a query within a query (sub querying).
We are applying filter on UserEducationDetails model where rank is “first” and on user model where first_name starts with “ki” In Prefetch the first parameter (“education_details”) passed is the related_name
name,The second parameter (queryset=UserEducationDetails.objects.filter(rank=”first”))
is the subquery which is actual queryset for the Prefect,And the third parameter (to_attr=”user_education_details”,) is optional,
if you don’t pass you can access data from “education_details”
And since we have passed it here we can access it from user_education_details.