Django Models (original) (raw)

Last Updated : 2 May, 2026

A Django model is a Python class that represents a database table. It helps manage data easily using Python instead of writing SQL.

database

Django Models

Creating a Django Model

A Django project and app are needed before defining any models. After starting an app, models can be created in the file app_name/models.py.

In **models.py:

Python `

from django.db import models

class GeeksModel(models.Model): title = models.CharField(max_length=200) description = models.TextField() image = models.ImageField(upload_to='images/')

def __str__(self):
    return self.title

`

This defines a Django model called "GeeksModel" which has three fields:

This model is converted into a database table through Django’s migration system, which tracks changes and applies them to the database. Django maps the fields defined in Django models into table fields of the database.

django-models

Database Table

Applying Migrations

Whenever a model is created, deleted or updated in the project’s models.py file, the following commands must be executed:

python manage.py makemigrations
python manage.py migrate

The migrations system in Django generates and applies the necessary SQL commands to update your database according to the changes in your models.

Registering Models in Django Admin

To manage your model via the admin interface:

  1. Open admin.py in app directory.
  2. Register the model: Python `

from django.contrib import admin

Register your models here.

from .models import GeeksModel

admin.site.register(GeeksModel)

`

This code does not produce any output, it simply registers the "GeeksModel"with the admin site, so that it can be managed via the Django admin interface. The admin panel can now be used to create, update and delete model instances.

Model Field Types

Django models consist of fields that define the structure of database tables. Each field specifies:

**Common field types include:

Fields not only define how data is stored, but also automatically handle validation and form generation when used in Django forms.

Relationship Fields

Relationship fields are used when models are related to each other. It allow to represent one-to-one, one-to-many and many-to-many relationships in your database. Common relationship fields:

Relationship fields allow easy querying of related objects using Django ORM and automatically handle database constraints.

Field Options

Every field in Django models can have options (attributes) to control its behavior. Field options allow you to:

These options make Django models highly flexible and ensure data integrity in the database and forms.

CRUD Operations Using Django ORM

Django's Object-Relational Mapper (ORM) allows interaction with the database using Python code instead of raw SQL. With the ORM, four basic operations on models are:

The ORM abstracts away SQL commands, making database operations more readable, organized and consistent across different databases.