KEMBAR78
Django model create a table in django web framework | PPTX
DJANGO
MODEL
DJANGO WEB FRAMEWORK
WHAT IS DJANGO MODEL?
A Django model is a table in your database.
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 Database one uses with
Django.
DJANGO MODEL
The basics of a model include –
1. Each model is a Python class that subclasses django.db.models.Model.
2. Each attribute of the model represents a database field.
3. With all of this, Django gives you an automatically-generated
database-access API Django provides built-in database by default that
is sqlite database.
4. We can use other database like MySQL, Oracle SQL etc.
CREATE OWN MODEL CLASS
• models.py file which is inside application folder, is required to
create our own model class.
• Our own model class will inherit Python’s Model Class.
Syntax:
class ClassName(models.Model):
field_name=models.FieldType(arg, options)
EXAMPLE: MODEL
CLASS(MODELS.PY FILE)
from django.db import models
class student_data(models.Model):
stud_name=models.CharField(max_l
ength=50)
course_name=models.CharField(ma
x_length=50)
Python class
Own class
Fields
Field Types
RULES FOR FIELD
1. Field Name instantiated as a class attribute and represents a particular
table’s Column name.
2. Field Type is also known as Data Type.
3. A field name cannot be a Python reserved word, because that would result
in a Python syntax error.
4. A field name cannot contain more than one underscore in a row, due to the
way Django’s query lookup syntax works.
5. A field name cannot end with an underscore.
HOW TO USE MODELS
Once you have defined your models, you need to tell Django you’re going to use
those models.
•Open settings.py file
•Write app name which contains models.py file in INSTALLED_APPS = [ ]
•Open Terminal
•Run python manage.py makemigrations
•Run python manage.py migrate
MODEL AND DATABASE TABLE
Model Name student_data
Fileld name 1 stud_name
Field name 2 course_name
Id stud_name course_name
TABLE NAME : formapp_student_data
Application name Model name
MIGRATIONS
Migrations are Django’s way of propagating changes you make to your
models (adding a field, deleting a model, etc.) into your database schema.
makemigrations – This is responsible for creating new migrations based
on the changes you have made to your models.
migrate – This is responsible for applying and unapplying migrations.
sqlmigrate – This displays the SQL statements for a migration.
showmigrations – This lists a project’s migrations and their status.
COMMANDS
makemigrations is used convert model class into sql statements.
This will also create a file which will contain sql statements. This
file is located in Application’s migrations folder.
Syntax:- python manage.py makemigrations
COMMANDS
migrate is used to execute sql statements generated by makemigrations.
This command will execute All Application’s (including built-in applications)
SQL Statements if available. After execution of sql statements table will be
created.
Syntax:- python manage.py migrate
Note – If you make any change in your own model class you are
required to run makemigrations and migrate command only then you
will get those changes in your application.
SHOW TABLE DATA TO USER

Writing Code to get data from database in views.py then pass it to template
files using render function.
Get Data which is passed by render function of views.py file in template file.
all ( ) – It returns a copy of current QuerySet or QuerySet Subclass.
Syntax:- ModelClassName.objects.all( )

Django model create a table in django web framework

  • 1.
  • 2.
    WHAT IS DJANGOMODEL? A Django model is a table in your database. 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 Database one uses with Django.
  • 3.
    DJANGO MODEL The basicsof a model include – 1. Each model is a Python class that subclasses django.db.models.Model. 2. Each attribute of the model represents a database field. 3. With all of this, Django gives you an automatically-generated database-access API Django provides built-in database by default that is sqlite database. 4. We can use other database like MySQL, Oracle SQL etc.
  • 4.
    CREATE OWN MODELCLASS • models.py file which is inside application folder, is required to create our own model class. • Our own model class will inherit Python’s Model Class. Syntax: class ClassName(models.Model): field_name=models.FieldType(arg, options)
  • 5.
    EXAMPLE: MODEL CLASS(MODELS.PY FILE) fromdjango.db import models class student_data(models.Model): stud_name=models.CharField(max_l ength=50) course_name=models.CharField(ma x_length=50) Python class Own class Fields Field Types
  • 6.
    RULES FOR FIELD 1.Field Name instantiated as a class attribute and represents a particular table’s Column name. 2. Field Type is also known as Data Type. 3. A field name cannot be a Python reserved word, because that would result in a Python syntax error. 4. A field name cannot contain more than one underscore in a row, due to the way Django’s query lookup syntax works. 5. A field name cannot end with an underscore.
  • 7.
    HOW TO USEMODELS Once you have defined your models, you need to tell Django you’re going to use those models. •Open settings.py file •Write app name which contains models.py file in INSTALLED_APPS = [ ] •Open Terminal •Run python manage.py makemigrations •Run python manage.py migrate
  • 8.
    MODEL AND DATABASETABLE Model Name student_data Fileld name 1 stud_name Field name 2 course_name Id stud_name course_name TABLE NAME : formapp_student_data Application name Model name
  • 9.
    MIGRATIONS Migrations are Django’sway of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. makemigrations – This is responsible for creating new migrations based on the changes you have made to your models. migrate – This is responsible for applying and unapplying migrations. sqlmigrate – This displays the SQL statements for a migration. showmigrations – This lists a project’s migrations and their status.
  • 10.
    COMMANDS makemigrations is usedconvert model class into sql statements. This will also create a file which will contain sql statements. This file is located in Application’s migrations folder. Syntax:- python manage.py makemigrations
  • 11.
    COMMANDS migrate is usedto execute sql statements generated by makemigrations. This command will execute All Application’s (including built-in applications) SQL Statements if available. After execution of sql statements table will be created. Syntax:- python manage.py migrate Note – If you make any change in your own model class you are required to run makemigrations and migrate command only then you will get those changes in your application.
  • 12.
    SHOW TABLE DATATO USER  Writing Code to get data from database in views.py then pass it to template files using render function. Get Data which is passed by render function of views.py file in template file. all ( ) – It returns a copy of current QuerySet or QuerySet Subclass. Syntax:- ModelClassName.objects.all( )