top of page

Django model

Updated: Mar 2, 2022

Django model


In this blog we will discuss django form. What is it and their use? Let's see.


What is Django module


Django's built-in tool for creating tables, their fields, and other restrictions is called a Django model. In a nutshell, Django Models is the database SQL that is used with Django. SQL (Structured Query Language) is a complicated language that includes a variety of queries for generating, removing, updating, and other database-related tasks. Django models make jobs easier to complete and organise tables into models. In most cases, each model corresponds to a single database table.


Django models to store data in a database with ease. Furthermore, we may utilise Django's admin panel to add, update, delete, or retrieve model fields, among other things. Simplicity, consistency, version control, and comprehensive metadata management are all features of Django models.


Model class


Model class which will represent a table in database. Each model is a python class that subclassess django.db.models.Models. Each attributes of the model represent a database fields. With all of this django gives you automatically generate database access API djnago provides built in database by default that is sqlite database. we can use other database such as Mysql and Oracle SQL etc.


Create our own model class

After creating the django project. model.py file which is inside the application folder, which is required to create our own model class. Our own model class will inherit python model class.


syntax :


class ClassName(model.Model):

field_name=models.FieldType(arg,options)


Example :

Code :


from django.db import models
from django.contrib.auth.models import User
# Create your models here.

STATUS = ((0, "Draft"), (1, "Published"))

class Post(models.Model):
  title =  models.CharField(max_length=200, unique=True)
  slug =   models.SlugField(max_length=200, unique=True)
  author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
  created_on = models.DateTimeField(auto_now_add=True)
  updated_on = models.DateTimeField(auto_now=True)
  content = models.TextField()
  status = models.IntegerField(choices=STATUS, default=0)

  class Meta:
    ordering = ['-created_on']

  def __str__(self):
    return self.title
  • Field Name instantiated as a class attribute an represent a perticular table column name

  • Field Type is also know as Data type.

  • A field name cannot be a python reserved word, because that would result in a python syntax error,

  • A field name cannot contain more than one underscore in a row, due to the way django query lookup syntax works.

  • a field name cannot end with underscore.

How to use


Once defined the model, we need to tell django you are going to use those models.

How Codersarts can Help you in Django ?


Codersarts provide:


  • Django Assignment help

  • Django Error Resolving Help

  • Mentorship in Django from Experts

  • Dajngo Development Project

If you are looking for any kind of Help in Django Contact us


Comments


bottom of page