A Django model is the built-in feature that Django uses to create SQL Database. SQL involves a lot of different complicated queries to Add, Create or Modify the Database. And here is where Django Models turn out to be really helpful. Django simplifies the task for us to create and manage the Databases.
from django.db import models class Passengers(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)
In this article, we will look into…
- How to Create a Django Model
- Add Attributes to the Models to represent a database field
- Migrate the Models
- Managing Models with Python Shell
- Managing Models with Django Admin Interface
Now that you know what we are going to do, let’s jump right in
Creating Django Models
#import Models from django.db from django.db import models
First of all, you have to import models from Django.db into the models.py file
Although this import is been taken care of by Django
Create a New Model
#import models from django.db from django.db import models class Passengers(models.Model): pass
Now we have successfully created our new Django Model, for now, let’s only have pass inside the model class
#import models from django.db from django.db import models class Passengers(models.Model): name = models.CharField(max_length=64)
Now we use this code to create a new character field for our model named name.
The parameter named max_length would create a limit for the number of characters that can be entered.
#import models from django.db from django.db import models class Passengers(models.Model): name = models.CharField(max_length=64) description = models.TextField()
After that create a Text Field for description, although it is so absurd to ask passengers for a description
Hence let’s give it a parameter, blank = True , which simply means the user can leave this field blank
The difference between CharFields and TextFields is that in CharField we give a limit parameter, whereas TextField is used in the case where the user enters a multiline text.
#import models from django.db from django.db import models class Passengers(models.Model): name = models.CharField(max_length=64) description = models.TextField(blank=True) departing_time = models.DateTimeField()
Now for the Date and Time let’s use DateTime
For automatically filling current date and time, we must use another parameter,
Whenever we make changes to our models.py, we have to migrate the models to see them in action. For this, we use 3 commands
First we makemigrations, we do this by running,
python3 manage.py makemigrations
After that, to check if we actually made the migrations, we must run
python3 manage.py showmigrations
Then the last step, migrate!
python3 manage.py migrate
Managing Django Models with Python Shell
That’s right, you can access and manage your Django Models within the python shell
To do this, first, we have to get into python shell, we do this by running the command
python3 manage.py shell
Once we’re in, we would have to import the model into the shell
from myapp.models import Passengers
Here, myapp is the app name and Passengers is the model’s name
Next, to add data, we must create a variable and store the value
passenger1 = Passengers(name="Geekalgo", description="")
We have successfully added data to our models using shell
Managing Models with Django Admin Interface
To render a model in Django admin, we need to modify
app/admin.py. With Django’s admin interface, managing models become much simpler.
Register Django Models in Admin.py
Go to admin.py in myapp and enter the following code. Import the corresponding model from models.py and register it to the admin interface.
from django.contrib import admin from .models import Passengers admin.site.register(Passengers)
Create Super User
By creating a superuser, you can log into Django’s Admin interface
python3 manage.py createsuperuser
Using Django’s Admin Interface
Run your Django server and go to http/127.0.0.1:8000/admin
Login with the superuser credentials you just created
Hurray! Now you are in your admin interface
To Sum Up
Today we discussed how to Create a Django Model, Add Attributes to the Models to represent a database field, Migrate the Models, Managing Models with Python Shell as well as with Django Admin Interface
If anything didn’t go the way it should, or, you got some errors and so couldn’t move ahead, feel free that Comment Down below, or make use of our Forums
If you are on your way to becoming a Django Developer, you might find our site really helpful. Hit this link for more Django related posts
Check it out, it’s Simply the Best Code.