READ Operation
Now, we are going to learn how to perform READ operations using django ORM.
First of all, import Center, Vaccine, and Campaign Model.
>>> from center.models import Center
>>> from vaccine.models import Vaccine
>>> from campaign.models import Campaign
Now, to get all the data stored in the center table, we need to use all method.
>>> Center.objects.all()
<QuerySet [<Center: ABC Hospital>, <Center: XYZ Hospital>]>
By executing Center.objects.all(), it returns a queryset that contains all the center objects stored in the database.
Now, one thing which I forgot to mention in the last section is the primary key. By default, django adds a new database field named "id" if you dont specify the primary key of your model.
Since, we have not mentioned primary key in any of our models, so, django by default as added id field in each model that represents our primary key.
If we need to find out id of every center object we can use for loop to iterate over the queryset.
>>> for item in Center.objects.all():
... print(item.id)
...
1
2
Note: You might get a different id in the output and that's completely ok
Now, if you want to fetch a specific row from the database table using the primary key, then use the get method.
>>> center1 = Center.objects.get(id=1)
>>> center2 = Center.objects.get(id=2)
GET method takes the primary key or any unique field as an argument and returns an object not querysets.
While writing query using get method, make sure your query desn't returns two or more object.
If you write a query using get method that returns two or more objects, then django will throw an error.
Use get method to fetch only single object not multiple object.
Now, if you want to apply filter on your database values, you can use filter method.
In this example, we are filtering out all center objects whose name contains "Hospital" keyword.
>>> Center.objects.filter(name__contains = "Hospital")
<QuerySet [<Center: ABC Hospital>, <Center: XYZ Hospital>]>
In this example, we are filtering out all the center objects whose name contains 'Z' letter.
>>> Center.objects.filter(name__contains = "Z")
<QuerySet [<Center: XYZ Hospital>]>
In this example, we are filtering out all the center objects whose name startswith 'A' letter.
>>> Center.objects.filter(name__startswith="A")
<QuerySet [<Center: ABC Hospital>]>
In this example, we are filtering out all the center objects that endswith 'l' letter.
>>> Center.objects.filter(name__endswith="l")
<QuerySet [<Center: ABC Hospital>, <Center: XYZ Hospital>]>
So, this is an basic example of how you can query the database using django orm. There are lots of methods provided by the Django to query the database and it is really impossible to explain each one of it. So, I have attached the django documentation link where you can learn more about making queries.
[Browse in the latest version of Django]
Now, lets learn how to query the objects of Model that has many-to-one and many-to-many relationships.
First of all, start a new python shell and then import Center, Vaccine, Campaign and User model.
>>> from center.models import Center
>>> from vaccine.models import Vaccine
>>> from campaign.models import Campaign
>>> from django.contrib.auth import get_user_model
>>>
>>> User = get_user_model()
Now, we need to get the instance of all the objects which we have created so far.
>>>
>>> for item in Center.objects.all():
... print(item.id)
...
1
2
>>> center1 = Center.objects.get(id=1)
>>> center2 = Center.objects.get(id=2)
>>>
>>> for item in Vaccine.objects.all():
... print(item.id)
...
1
>>> vaccine1 = Vaccine.objects.get(id=1)
>>>
>>> for item in Campaign.objects.all():
... print(item.id)
...
1
>>> campaign1 = Campaign.objects.get(id=1)
>>>
>>> for item in User.objects.all():
... print(item.id)
...
1
2
>>> user1 = User.objects.get(id=1)
>>> user2 = User.objects.get(id=2)
In this way, we have got all the instances of objects which we have created till now.
Now, in the case of the Many-to-One and Many-to-Many relationship, we can query the database by following relationship forward and by following relationship backward.
So, if you write
>>> campaign1.center
<Center: ABC Hospital>
>>> campaign1.vaccine
<Vaccine: MyVaccine>
>>> campaign1.agents.all()
<QuerySet [<User: admin>, <User: user1>]>
Then this is the example of following relationship forward.
But if write the query in this way,
>>> center1.campaign_set.all()
<QuerySet [<Campaign: MYVACCINE | ABC HOSPITAL>]>
Then this is the example of following relationship backward.
Here, we are using center objects to find out all the campaign objects with which it has one-to-many relationship with center1 instance
So, we are fetching out all the campaign objects whose center is set to center1 instance.
Similarly,
>>> vaccine1.campaign_set.all()
<QuerySet [<Campaign: MYVACCINE | ABC HOSPITAL>]>
Here, we are querying all the campaign objects with which it has one-to-many relationships with vaccine1 instance.
Similarly, for many to many also, if we need to find out all the campaigns which the user1 is associated with then we can write this query.
>>> user1.campaign_set.all()
<QuerySet [<Campaign: MYVACCINE | ABC HOSPITAL>]>
Therefore, this is how you query the database by following relationships backward.