Django Models: Defining Guide

One way to define models in Django is by creating a class that inherits from django.db.models.Model and defining the model’s fields within that class. For example, the following is a simple example:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)
    publication_date = models.DateField()

    def __str__(self):
        return self.title

In the example above, we defined a model named Book with three fields: title, author, and publication_date. Title and author are fields of type CharField, while publication_date is a field of type DateField. We also defined a __str__ method for representing instances of the model.

bannerAds