30 Jul 2024 by Ijeoma Jahsway
Category: Web Development
Frameworks: Python, Django
Read Time: 10 Minutes
Django models are a powerful way to interact with a relational database. They provide a high-level abstraction for database operations, allowing developers to work with data in Pythonic ways.
A Django model is a Python class that subclasses django.db.models.Model
. Each attribute of the model represents a database field.
Here's a simple example:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=255)
author = models.CharField(max_length=255)
published_date = models.DateField()
isbn = models.CharField(max_length=13, unique=True)
pages = models.PositiveIntegerField()
def __str__(self):
return self.title
In this example:
title
, author
, and isbn
are character fields.published_date
is a date field.pages
is a positive integer field.Django offers a variety of field types and options to customize the database schema:
CharField
for short-to-mid-sized strings.TextField
for large text fields.IntegerField
, PositiveIntegerField
, FloatField
, and DecimalField
for numbers.DateField
and DateTimeField
for date and time values.ForeignKey
, OneToOneField
, and ManyToManyField
for relationships.Here’s how you can define relationships:
class Author(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=255)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
published_date = models.DateField()
def __str__(self):
return self.title
You can add custom methods to your models. For example, you might want a method that returns the full name of an author:
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
def full_name(self):
return f"{self.first_name} {self.last_name}"
Django’s admin interface is a powerful tool for managing data. To make your model available in the admin interface, you need to register it:
from django.contrib import admin
from .models import Book
admin.site.register(Book)
For more customized admin behavior, you can create a model admin class:
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'published_date')
search_fields = ('title', 'author__name')
admin.site.register(Book, BookAdmin)
Django models provide a powerful and flexible way to interact with your database. By understanding the basics and utilizing the various field types and options, you can create robust and efficient data models for your Django applications.
Happy coding!
-- https://docs.djangoproject.com/en/stable/ https://www.twoscoopspress.com/products/two-scoops-of-django-3-x --
Subscribe to our Telegram Channel to get exclusive content and be the first to know when new blog posts go live! 🎉
#Python #Django Tutorial #Django #Web Development #Programming #Backend Development #Django Guide #Django Models #Web Framework #Full Stack Development
Delta State, Nigeria