Updated May 22, 2023
Introduction to Django Model Fields
The following article provides an outline for Django Model Fields. We know that Django offers different functionality to the user, in which model field is one of the functionalities that Django provides. Typically, Django fields represent data types used to store specific data according to our requirements. For example, suppose we need to store integer values; then, we can use an integer field to store the integer value. However, Django provides built-in validation for all the specified data types, so we cannot store XYZ’s character value in the integer field. Similarly, we can store different kinds of data whenever required.
Overview of Django Model Fields
The main piece of a model and the main required piece of a model is the rundown of data set fields it characterizes. Class credits determine the fields. Avoid picking field names that are in contention with the model API, like clean, save, or erase. A model is the single, conclusive wellspring of data about your information. It contains the fundamental fields and ways of behaving with the information you’re putting away. By and large, each model guides to a binary data set table. With all this, Django gives you a naturally created data set admittance API.
It’s critical to comprehend how Django model information types work on the data set layer and the Django/Python layer. At the point when you characterize a Django model with its information types and make its underlying movement, Django produces and executes a model’s DDL (Data definition language) to make a data set table; This DDL makes decisions that mirror a Django model’s fields. For example, a Django IntegerField model field gets converted into an INTEGER DB section to uphold number qualities. That’s what this intends; assuming you change this Django model field (for example, changing a number field to a text field) requires another relocation for the data set table to mirror any new rule.
Notwithstanding this standard implementation made at the beginning by a Django model at the information base layer, Django model fields likewise uphold information rules at the Django/Python layer (for example preceding embedding/refreshing information in the data set). For instance, Django model field boundaries like decisions implement a field’s qualities to adjust to many decisions. This rule is upheld when you endeavor to make, save or update Django model records and is done independently of the information base table standards. This implies you can essentially change a Django model field related to this kind of rule and not need to make another relocation to modify the fundamental data set table for this situation; changing the Python code is sufficient to change approval conduct.
Django Model Fields Methods
Given below are the Django model fields methods:
- to_python() technique on a field is the initial phase in each approval. It forces the worth to the right data and raises ValidationError in the event that that is beyond the realm of possibilities.
- The validate() technique on a Field handles field-explicit approval that isn’t reasonable for a validator. It takes a value pressured to the right data type and raises ValidationError on any blunder.
- The run_validators() technique on a Field runs all of the field’s validators and totals every one of the mistakes into a solitary ValidationError. You may not have to abrogate this strategy.
- The clean() technique on a Field subclass is liable for running to_python(), approve(), and run_validators() all put together, proliferating their blunders. Whenever any techniques raise a ValidationError, the approval process stops, and the corresponding mistake is raised.
Django Model Fields Types
Given below are different field types of models:
- AutoField: This is an IntegerField, and it automatically increments.
- BihAutoField: It uses 64-bit integer values and automatically increments them.
- BigIntegerField: It uses 64-bit integer values and automatically increments them.
- BinaryField: It stores the raw binary data.
- CharField: It is a data type used to store text values.
- DataField: Basically, it is used in python and used to display DateTime.
- DecimalField: It holds fixed precision numbers.
- DurationField: Use to store periods.
- EmailField: Use to check whether an email is valid or not.
- FileField: Use to upload files.
- FloatField: Use to store the floating-point number represented in Python.
- ImageField: Use to upload images, and it is also used to validate the uploaded image.
- IntegerField: Use to store the integer values.
Django Model Fields List
Below is a list of the model fields available in Django:
- GenericiPAddressField: It is used to address the IPv4 or IPv6.
- NullBooleanField: It stores the Boolean value but permits a null value.
- PositiveIntegerField: It only holds the integer value, which means either zero or positive.
- PositiveSmallIntegerField: By using this field, we can store only specific values.
- StugField: It provides a short label for a particular term containing underscores, letters, and numbers.
- TimeField: Use to display time and date in Python.
- TextField: Use to store text.
- URLField: Use to validate the URL with the help of URLValidator.
- UUIDField: It stores the unique identifiers over the universal structure.
Examples of Django Model Fields
Given below are the examples of Django Model Fields:
Suppose we need to create a student model, and inside that model, we have two fields: studname and studcity.
from django import models
Studname = models.CharField(max_length=50)
Studcity = models.CharField(max_length=50)
The above implementation in SQL looks like the one below.
create table student("id" Not Null Primary Key, "studname" varchar(50) NOT NULL ,"studcity" varchar(50) Not NULL);
We consider the above example and try to fetch all students’ names, as shown in the screenshot below.
Let’s see another example; suppose we need to search the student base in the city.
With the help of the above article, we saw the Django model fields. From this article, we saw basic things about the Django model fields and the features and examples of the Django model fields, and how we use them in the Django model fields.
We hope that this EDUCBA information on “Django Model Fields” was beneficial to you. You can view EDUCBA’s recommended articles for more information.