Django Models For Beginners
Introduction
When I first got started with Django, one of the concepts that I struggled to understand and implement initially was models. I didn’t quite understand the purpose of models and how powerful they could be if used correctly. In this article, I want to give anyone new to Django or getting started with models, a basic understanding of what they are, how you can use them, and what you can do with them.
What are models in Django?
‘A model is the single, definitive source of information about your data. It contains the essential fields and behaviours of the data you’re storing. Generally, each model maps to a single database table.’
— Source: Django Documentation.
In essence, a model is Django's way of interfacing between the user and the database, it provides developers with a means of creating, reading, updating and deleting information in the database from the front end of the application. This system of implementing data changes is fantastic for newer developers as it mitigates the process of updating data tables in your database, allowing newer programmers to work more on the development of their applications than worrying about how to structure and append their data.
So, how do we use models?
With models, it can be very dependent on what you are trying to do but in general, the first thing that you will want to do is head over to your models.py file, in here we can create a basic model as follows.
from django.db import modelsdef example_model(models.Model):
firstname = models.CharField(max_length=50) lastname = models.CharField(max_length=50) job = models.CharField(max_length=50) location = models.CharField(max_length=75)
In the example above, we have created a new model with the name ‘exmaple_model’ which takes in the parameter ‘models.Model’ which is a subclass of django.db.models.Model. We then provide the names of our fields, think of these as the column names in our data table, which we also provide with a field type. Depending on whether you want a text field, numerical field, or date field you can add these here and the model will only accept these datatypes as inputs, you can also provide validation rules to these fields such as the ‘max_length’ rule seen in the example above.
We can then use our model in our forms.py to be used when populating data into our data table, as follows.
from django.forms import ModelFormfrom .models import exmaple_model
class RegistrationForm(ModelForm): class Meta: model = exmaple_model fields = ["firstname", "lastname", "job", "location"]
As you can see in the example above, we use the model we created in our form which will be used to display input fields on the front end of the website to our users. The fields in our form represent the columns in our database which we created in our model.
And with that, we can use this form wherever we like, for example, we could create a model to hold user information and then use that model in a form, which couldn't then use to log in or register our users, an example of this use case can be found in the article below.
Why use Django models?
Models provide a low-level way of creating, reading, updating, and deleting data in your Django database, they are quick and easy to use and are perfect for newer developers, the process gives the user a basic understanding of data manipulating and structuring and provides scalability to the user's project.
With that, you have everything you need to know to setup a basic model in your Django application and use it to manipulate data. If you have any questions, please feel free to ask. Happy coding!