1.

What are models in Django?

Answer»

A model in Django refers to a CLASS that MAPS to a database table or database collection. Each attribute of the Django model class represents a database field. They are defined in app/models.py

Example: 

from django.db import modelsclass SampleModel(models.Model):field1 = models.CharField(max_length = 50)field2 = models.IntegerField()class Meta:db_table = “sample_model”

Every model inherits from django.db.models.Model

Our example has 2 attributes (1 char and 1 integer field), those will be in the table fields.

The metaclass helps you set things like available permissions, singular and plural VERSIONS of the name, associated database table name, whether the model is abstract or not, etc.

To get more information about models you can refer here: https://docs.djangoproject.com/en/3.1/topics/db/models/.



Discussion

No Comment Found