A Guide on Django Models
Django models
Django models are the heart of the Django framework. They are used to define the structure of the database and the relationships between different models. In this section, we will explore the basics of Django models and how to create them.
Defining a model
So far, we have created a same chai app and it’s time to add some data to it. To do this, we need to define a model. A model is a Python class that represents a table in the database. It contains fields that define the structure of the table and methods that define the behavior of the table.
To define a model, we need to use the models.py
file in our Django project. Add the following code to the models.py
file:
In this code, we have defined a ChaiVariety
model with the following fields:
name
: ACharField
that stores the name of the chai variety.image
: AnImageField
that stores the image of the chai variety.date_added
: ADateTimeField
that stores the date and time when the chai variety was added.type
: ACharField
that stores the type of the chai variety (e.g., ‘ML’, ‘GR’, ‘KL’, ‘PL’, ‘EL’).description
: ATextField
that stores the description of the chai variety.
The __str__
method is used to return a string representation of the object. In this case, it returns the name of the chai variety.
Since we are using image field, we need to install Pillow library to use it.
Then we need add some settings to our settings.py
file to use the image field.
Now, let’s configure our projects urls.py file to reflect media files.
Adding data to the database
Now that we have defined our model, we can add some data to the database. Lets migrate the database and add some data to the ChaiVariety
model.
Now, let’s add some data to the database. You can go to admin.py
file and add the following code to the ChaiVariety
model.
Now, go to the admin page and you should see the ChaiVariety
model listed. Add some data to the model and save it.
Creating a view to display the data
Now that we have added some data to the database, we can create a view to display the data. Go to the views.py
file and add the following code.
Get data in the template
In the all_chai.html
template, we can use the chais
variable to display the data. Add the following code to the all_chai.html
template.
adding description to the model and details page
We can add a description to the ChaiVariety
model by adding a description
field to the model. Add the following code to the models.py
file.
Adding a details view
Now that we have added a description to the ChaiVariety
model, we can create a view to display the details of a specific chai variety. Go to the views.py
file and add the following code.
In the all_chai.html
template, we can use the url to display the details of a specific chai variety.
Configuring the urls.py file
Now that we have created a view to display the details of a specific chai variety, we need to configure the urls.py file to reflect this. Go to the urls.py
file and add the following code.
Create chai_detail.html
template
Now that we have created a view to display the details of a specific chai variety, we need to create a template to display the details. Go to the chai
folder and create a new file called chai_detail.html
. Add the following code to the chai_detail.html
template.
Conclusion
In this section, we have learned how to create a model, add data to the database, create a view to display the data, and create a view to display the details of a specific chai variety. We have also learned how to configure the urls.py file to reflect the views and how to create a template to display the details. With these concepts, we can create a complete web application that allows users to add and view chai varieties.
Now go ahead and subscribe to both of our youtube channels. Links are available at home page. Enjoy the tea and Django!