A simple example would be for a library management application; you would have 2 models, for example, student
and book
in models.py:
from django.db import models
class student(models.Model):
roll_no = models.IntegerField(primary_key=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
Here we have given roll_no a primary key to the student model, but even if we don't give a primary key to any attribute, Django would automatically assign an attribute called id
, which would be automatically assigned and incremented on the creation of new rows.
Now you can just import this model into your views
or in a project and interact with it by simply creating an object of that model.
Django has many inbuilt Fields available, or even you can create your own as well.
Django also supports relationships between models, many-to-many
, one-to-one
, many-to-one
.
Django's detailed doc for Models