Skip to content

CREATE Operation

Resources:
- Section 5 Code Files

Now, let us learn how to perform CREATE operations using Django ORM.

  • First of all, you need to download the project code files from the resources section.

  • Now, extract the folder on the desktop.

  • Then, copy the mysite folder which is the project root directory.

  • Replace the mysite folder of your project with the copied mysite folder. So, if you made any mistake while writing the code in the previous section, then, it won't affect your learning in this section.

  • Open your project in the VS code and in the terminal, activate the virtual environment, and move to the mysite.

  • Then, migrate the changes to the SQLite database.

python manage.py migrate
  • Also, create a new superuser account to log in to admin panel.
python manage.py createsuperuser

Now, in the terminal open the python shell by executing this command.

python manage.py shell

First of all, we need to import Center, Storage, Vaccine and Campaign model in our python terminal.

>>> from center.models import Center
>>> from vaccine.models import Vaccine

Actually, there are 2 ways to create an object and store it in the database.

The first way is directly creating an object and then using some command to store it in the database.

So, in this code, we are creating an object of Center in the same way we create an object from any regular python class.

>>> center1 = Center(name="ABC Hospital", address="somewhere city, 12345, Earth")
>>> center1
<Center: ABC Hospital>

This object has two attributes name and address.

>>> center1.name
'ABC Hospital'
>>> center1.address
'somewhere city, 12345, Earth'

But the problem with this approach is, Django doesn't save this center object directly in the database.

So, if you execute this code which is given below, you will get an empty queryset.

>>> Center.objects.all()
<QuerySet []>

Now, currently don't worry about what this code means, it just returns all the center objects which are stored in the database.

So, till now, the center object which we have created is not stored in our database. Therefore, to store this center object in our database we need to invoke the save method.

>>> center1.save()

Now, if you execute this command, you will get a queryset containing Center object which you have created.

>>> Center.objects.all()
<QuerySet [<Center: ABC Hospital>]>

There is another way to create an object and that is using a model manager.

In Django ORM, a Model Manager is an interface through which database query operations are provided to Django models.

At least one Manager exists for every model in a Django application and the name of that model manager is objects.

So, we will use the model manager named "objects" to create another center object.

>>> center2 = Center.objects.create(name="XYZ Hospital", address="somewhere city, 67890, Earth")

In this code, we are using create method of model manager to create a new object in the model manager.

When we use model manager, we don't need to invoke save method. Django automatically saves the newly created object in the database.

Now, if you execute this code, you will get two different center objects.

>>> Center.objects.all()
<QuerySet [<Center: ABC Hospital>, <Center: XYZ Hospital>]>

In this code, we are using all method of model manager to get all the center objects from the database.


Now, let us create an object for Vaccine using the model manager.

>>> vaccine1 = Vaccine.objects.create(
... name="MyVaccine",
... description="Covid19 Vaccine",
... number_of_doses = 2,
... interval=30,
... storage_temperature=10,
... minimum_age=18)

In this way, we can create a Vaccine object.

Now, if you execute this command, you will get all the vaccine objects present in the database.

>>> Vaccine.objects.all()
<QuerySet [<Vaccine: MyVaccine>]>

After that, exit the python shell.

exit()

Run the development server and then open the django admin panel.

Create a new user named "user1" from the admin panel.

Come back to the terminal, stop the development server and start the python shell.

python manage.py shell

First all, import the models which we need to execute our code.

>>> from vaccine.models import Vaccine
>>> from center.models import Center

To import the user model use get_user_model function from django.contrib.auth

>>> from django.contrib.auth import get_user_model
>>> User = get_user_model()

Now, we are going to create an object for campaign. If you notice carefully, there is a many-to-one relationship between campaign and center as well as campaign and vaccine. Similarly, there is a many-to-many relationship between campaign and user model. So, to create campaign object, we require center, vaccine and user objects.

>>> Center.objects.all()                             
<QuerySet [<Center: ABC Hospital>, <Center: XYZ Hospital>]>
>>> center1 = Center.objects.all()[0]  

In this way, we have got the reference to ABC center in this terminal.

>>> Vaccine.objects.all()
<QuerySet [<Vaccine: MyVaccine>]>
>>> vaccine1 = Vaccine.objects.all()[0]

In this way, we have got the instance of MyVaccine in this terminal.

Now, currently we have two users in the database and they are admin and user1.

>>> User.objects.all()
<QuerySet [<User: admin>, <User: user1>]>
>>> user1 = User.objects.all()[0]
>>> user1
<User: admin>

In this way, we have got the instance of admin object.

>>> user2 = User.objects.all()[1]
>>> user2
<User: user1>

And now, we have got the instance of user1 object.

Now, we are going to learn how to create an object of a model that follows many-to-one and many-to-many relationships.

First of all, import Campaign model

>>> from campaign.models import Campaign

Now, create a campaign object.

>>> campaign1 = Campaign.objects.create(center = center1, vaccine = vaccine1)

Here, we are writing center = center1, which means that this campaign object will store the id of center1 in the center column since it is using foreign-key as model field type

Likewise, we are writing vaccine = vaccine1, which means that this campaign object will store the id of vaccine1 in the vaccine column since it also using foreign-key as model field type.

In this way, we have learnt how to create an object for a model which has a many-to-one relationship.

>>> campaign1
<Campaign: MYVACCINE | ABC HOSPITAL>

Now, if you execute campaign1.center it will return the center with which it is associated via a many-to-one relationship.

>>> campaign1.center
<Center: ABC Hospital>

Similarly, if you also execute campaign1.vaccine it will return the vaccine with which it is associated via a many-to-one relationship.

>>> campaign1.vaccine
<Vaccine: MyVaccine>

Finally, lets discuss about many-to-many relationships. Actually, we can't directly assign users to agents just like we did in center and vaccine field.

For many-to-many relationship, we need to use add() method.

>>> campaign1.agents.add(user1, user2)

This line of code adds user1 and user2 to the agents of campaign1 object and this is how you need to perform add operations in

So, if you execute this line of code, you will get all user objects which are associated with the campaign object.

>>> campaign1.agents.all()
<QuerySet [<User: admin>, <User: user1>]>

In this way, we have learned how to perform create operations using django ORM.