1 - Introduction To Django Model and Database PDF
1 - Introduction To Django Model and Database PDF
to
Django
1
Agenda
01
What is a Web Framework?
02
What is a Django?
03
History of Django
04
Features of Django
05
Django Installation
2
What is Web Framework?
3
What is Django?
4
History
5
Version Date Description
Versatile
Rapid Development
8
Django Installation
10
$ pip3 install django==2.0.3
11
Verify Django Installation
After installing Django, we need to verify the installation. Open terminal and
write python3 and press enter. It will display python shell where we can
verify django installation.
12
Django Project
13
Django Project Example
Here, we are creating a project djangpapp in the current directory.
14
Locate into the Project
Now, move to the project by changing the directory. The
Directory can be changed by using the following command.
cd djangpapp
15
To see all the files and subfolders of django project, we can use
tree command to view the tree structure of the application. This
is a utility command, if it is not present, can be downloaded via
apt-get install tree command.
16
Running the Django Project
17
18
Look server has started and can be accessed at localhost with
port 8000. Let's access it using the browser, it looks like the
below.
19
DJANGO
MODELS AND
DATABASE
20
Agenda
21
What is Model?
❑ A model is the single, definitive source of information about
your data.
22
CREATE YOUR FIRST MODEL
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
23
MODEL FIELDS
Field
Field Type Relationship
Options
24
1. FIELD TYPE
The fields defined inside the Model class are the columns name
of the mapped table
E.g.
DateField()
A date field
represents
python
datetime. date
instance. 25
2. FIELD OPTIONS
E.g.
26
The following are some common and mostly used field option:
01 Null
02 Blank
05 unique_key
03 default 04 primary_key
puts unique key
constraint for
store default this field will be column.
value for a field the primary key
for the table
27
3. MODEL FIELD RELATIONSHIP
1. many-to-one
2. many-to-many
3. one-to-one.
28
1) Many-to-one relationships:
E.g.
class Manufacturer(models.Model)
pass
class Car(models.Model):
manufacturer = models.ForeignKey(Manufacturer,
on_delete=models.CASCADE)
29
2) Many-to-many relationships
30
from django.db import models
class Topping(models.Model):
# ...
pass
class Pizza(models.Model):
# ...
toppings = models.ManyToManyField(Topping)
31
3) One-to-one relationships
E.g.
class MySpecialUser(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
supervisor = models.OneToOneField(settings.AUTH_USER_MODEL)
32
Meta Option
❑ A metaclass is the class of a class.
33
E.g.
class Student(models.Model):
name = models.CharField(max_length =50)
class Meta:
ordering =["name"]
db_table = "students"
34
Databases
Django officially
supports the following
databases:
35
Telling Django About Your Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': DATABASE_PATH,
}
}
36
Also create a new variable called DATABASE_PATH and add
that to the top of your settings.py
37