Introduction to Django Models
Introduction to Django Models
Django models are a crucial component of applications built using Django, a high-level Python Web framework. A model in Django is a special kind of object – it is saved in the database. A model is a definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing.
In simpler terms, Django models are the source of information for your data. They contain the crucial fields and behaviors of the data you're storing in your web application.
What is a Django Model?
A Django model is basically a Python class that inherits from the django.db.models.Model
class. Each attribute of the model represents a database field. Django gives you an automatic, database-access API; your model's class methods are transformed into database queries.
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
publication_date = models.DateField()
In this example, Book
is a Django model, with a title
and a publication_date
. The fields CharField
and DateField
are field types from Django's types of database fields, which tell Django what kind of data each field holds.
Field Types
Django uses the model field types to perform a lot of behind-the-scenes work for you. For example, it can automatically create forms based on your models, and it uses the field types to interact with the database.
Some commonly used field types are:
CharField
: A field for storing character data. The mandatorymax_length
argument provides the maximum size of the field.IntegerField
: A field for storing integer (whole number) data.DateField
andDateTimeField
: Fields for storing dates and date & time data respectively.TextField
: A field for storing large amounts of textual data.BooleanField
: A field for storing True/False values.
Creating a Model
To define your model, you create a class that subclasses django.db.models.Model
. Each model has a number of class variables, each of which represents a database field in the model.
Here is an example of a simple model:
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
In this example, the Publisher
model has six fields, each of which is represented by an instance of a Field
subclass.
The Power of Django Models
One of the most powerful parts of Django is the automatic admin interface. It reads metadata in your models to provide a quick, model-centric interface where trusted users can manage content on your site.
The admin’s recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around.
Migrations
Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They’re designed to be mostly automatic, but you’ll need to know when to make migrations, when to run them, and the common problems you might run into.
python manage.py makemigrations
python manage.py migrate
makemigrations
creates new migrations based on the changes detected on your models, and migrate
applies or unapplies migrations.
Conclusion
Django models are a powerful and flexible tool for managing your application's data. From defining simple fields, to running complex queries on the data, Django models have you covered. With the help of Django's automatic admin interface and migration tools, you can focus more on developing your application and less on the specifics of handling your data.