Advertisement



< 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.




Fields


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
AutoField
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.

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

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

BinaryField
A field used to store raw binary data.

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

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

DateField
A date field represented in Python by a datetime.date instance.

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

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

DurationField
A field for storing periods of time.

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

FileField
A file-upload field.

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

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

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

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

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

GenericIPAddressField
An IPv4 or IPv6 address, in string format.

NullBooleanField
Same as BooleanField with null=True.

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

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

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

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

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

URLField
A URL field.

UUIDField
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.

models.py
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.name + ', ' +  self.address + ', ' +  self.city + ', ' +  self.state + ', ' + self.country)

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




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement