Create models in Django

Here are some examples that can be used to create models in Django. Models in Django are used to create, update and retrieve data by connecting them to databases.

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=30)
    experience = models.IntegerField()
    is_active = models.BooleanField()
    auto_field = models.AutoField() //INTEGER FIELD THAT automatically increments
    emaill = models.EmailField(max_length=100) //CharField that checks - provided value is a valid email
    description = models.TextField() //Used to store large text value
    created_date = models.DateTimeField()

You will have to import models form 'django.db' to create a new model and then use models. Model while creating a new class. Django provides different types of data types like CharField, Integer, etc which will help you to design your database.

We have added different types of data type fields in the code snippet like the Integer field, Boolean field, Character field, etc. You can use them according to your requirements.

Was this helpful?