Tuesday, July 2, 2024
HomeLanguagesPythonModel Fieldname restrictions in Django Framework

Model Fieldname restrictions in Django Framework

A Django model is the built-in feature that Django uses to create tables, their fields, and various constraints. In short, Django Models is the SQL of Database one uses with Django. SQL (Structured Query Language) is complex and involves a lot of different queries for creating, deleting, updating or any other stuff related to database. Django models simplify the tasks and organize tables into models. 

This article revolves around restrictions on models field names.

Django places some restrictions on Model Field Names.

First create the django project to see this restrictions

django-admin startapp myproj
cd myproj

Then create new app.

python manage.py startapp main

Add main app in settings.py inside the INSTALLED_APPS

Restrictions on Field Name – 

1. Field name cannot be a python reserved word

Example 1

Python3




from django.db import models
  
# Create your models here.
class Student(models.Model):
    pass = models.CharField(max_length=100)


Error:

Example 2

Python3




from django.db import models
  
# Create your models here.
class Student(models.Model):
    global = models.CharField(max_length=100)


2. A field name cannot contain more than one underscore in a row

Python3




from django.db import models
  
# Create your models here.
class Student(models.Model):
    stu__name = models.CharField(max_length=100)


Error:

3. A field name cannot end with an underscore

Python3




from django.db import models
  
# Create your models here.
class Student(models.Model):
    stuname_ = models.CharField(max_length=100)


Error:

Last Updated :
16 Feb, 2021
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments