< Prev
Next >

Django Framework With Model

In this tutorial we are going to explain about the concept of model in Django Framework. A model is a Python class which corresponds to a database table. A model class contains data about your data.

Each model class contains one or more fields, where each field represents a database column in the same database table.


Each field of a model class is an instance of a specific type of Field class. Django uses the field class type to decide:
Let us see names and a short description of some of these Field classes provided by Django Framework.

Field Types

In the table below, you could see various Field types i.e. subclasses of Field class provided by Django Framework.

File Types Description
An IntegerField that automatically increments according to available IDs.

Note: Usually you won’t need to use this directly, a primary key field will automatically be added to your model if you don’t specify.

A 64-bit integer field, much like an AutoField except that it is guaranteed to fit numbers from 1 to 9223372036854775807.

A 64-bit integer field, much like an IntegerField except that it is guaranteed to fir larger numbers from -9223372036854775808 to 9223372036854775807.

A field used to store raw binary data.

A true/false field. The default value of BooleanField is None, when Field.default isn’t defined.

A string field, for small to large sized strings. For a large amounts of text, please use TextField class..

A date field represented in Python by a instance.

A date and time field represented in Python by a datetime.datetime instance.

A fixed-precision decimal number field, represented in Python by a Decimal instance.

A field for storing periods of time.

An e-mail field that checks that the value is a valid email address.

A file-upload field.

When you access a FileField on a model, you are given an instance of FieldFile as a proxy for accessing a file.

A file path field, whose choices are limited to the filenames in a certain directory on the filesystem.

A floating-point number field, represented in Python by a float instance.

A file-upload field which inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.

An integer field, which values from -2147483648 to 2147483647 in all databases supported by Django.

An IPv4 or IPv6 address, in string format.

Same as BooleanField with null=True.

Same as IntegerField, but must be either positive or zero (0) and it values from 0 to 2147483647 in all databases supported by Django.

Same as PositiveIntegerField, but only allows values from 0 to 32767 in all databases supported by Django.

Used to create a short label for something, containing only letters, numbers, underscores or hyphens. It is generally used in URLs.

Same as IntegerField, but only allows values from -32768 to 32767 in all databases supported by Django.

A time field which is represented in Python by a datetime.time instance.

A URL field.

A field for storing universally unique identifiers(UUID), it is based on Python’s UUID class.

An example of a Model class

As we have already told you that a model class corresponds to a database table on which we could perform database operations by using the Django Framework. Each model class is a subclass of django.db.model.Model class. So let us show you an example of a model class.
from django.db import models

# Creating our model class - Customer_Info

class Customer_Info(models.Model):
    name = models.CharField(max_length=30)
    dob = models.DateField(max_length=50)
    email = models.EmailField(max_length=30)
    age = models.IntegerField(max_length=50)
    married = models.BooleanField(max_length=30)

    def __str__(self):
        return ( + ', ' +  self.address + ', ' + + ', ' +  self.state + ', ' +

As you can see in the example above, we have created a Customer_Info model class with several fields, where each field is an instance of a different type of subclass of Field class. Each of these fields correspond to a column in the database table.

Each database field is represented by an appropriate Field class, such as -

Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition