...

/

Built-in Field Validation

Built-in Field Validation

Let’s learn about different built-in field validations.

Built-in Field Validations in Django models are the validations that are predefined in all Django fields. For example, IntegerField comes with built-in validation that it can only store integer values, and that it can only do so within a particular range. These validations are set in place so that invalid values are not submitted to the form. Django will show an error to let us know that an invalid value was submitted.

To understand the use of built-in validations, it’s best if we see them in action.

Modify the model

First, let’s modify our model a little bit. We added a count column in Access records.

Press + to interact
from django.db import models
from datetime import date
# Create your models here.
class Topic(models.Model):
top_name = models.CharField(max_length=264,unique=True)
def __str__(self):
return self.top_name
class Webpage(models.Model):
topic = models.ForeignKey(Topic,on_delete = models.CASCADE)
name = models.CharField(max_length=264, unique= True)
url = models.URLField(unique= True)
def __str__(self):
return self.name
class AccessRecord(models.Model):
name = models.ForeignKey(Webpage, on_delete= models.CASCADE)
date= models.DateField(default= date.today)
count= models.IntegerField(default=0)
def __str__(self):
return str(self.date)

Run the application

Since we have changed our schema, we have to migrate and register these changes, too. We have already done that, so hit the RUN button to ...