Open In App

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




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

Error:

Example 2




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




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




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

Error:


Article Tags :