0% found this document useful (0 votes)
142 views

1 - Introduction To Django Model and Database PDF

Django is a Python web framework that allows for rapid development of web applications. It uses the MVT (Model View Template) design pattern. Some key features of Django include being versatile, scalable, secure, and having a vast and supportive community. To install Django, you use pip to install the latest stable version. Once installed, you can create a Django project which generates the initial project structure and files. Models are used in Django to represent database tables and their rows. They are defined as Python classes that subclass django.db.models.Model. Each model attribute maps to a database field that can be customized using field types and options.

Uploaded by

create world
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

1 - Introduction To Django Model and Database PDF

Django is a Python web framework that allows for rapid development of web applications. It uses the MVT (Model View Template) design pattern. Some key features of Django include being versatile, scalable, secure, and having a vast and supportive community. To install Django, you use pip to install the latest stable version. Once installed, you can create a Django project which generates the initial project structure and files. Models are used in Django to represent database tables and their rows. They are defined as Python classes that subclass django.db.models.Model. Each model attribute maps to a database field that can be customized using field types and options.

Uploaded by

create world
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Introduction

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?

“ Web framework is a set of components designed to


simplify your web development process. It has basic
structuring tools in it, which serve as a solid base for
your project. It allows you to focus on the most
important details and project’s goals instead of creating

things, that you can simply pull out of the framework.

3
What is Django?

Django is a web application It takes less time to build


01 framework written in Python 04 application after collecting
programming language. client requirement.

It is based on MVT This framework uses a


02 (Model View Template) 05 famous tag line: The web
design pattern. framework for perfectionists
with deadlines.

The Django is very


03 demanding due to its rapid
development feature.

4
History

Publicly released 2.0 version is


under BSD license. launched

2003 2005 2008 2017 2018

Django was design 1.0 version is Its current stable


and developed by launched version 2.0.3 is
Lawrence journal launched.
world.

5
Version Date Description

0.90 16 Nov 2005

0.91 11 Jan 2006 magic removal

0.96 23 Mar 2007 newforms, testing tools

1.0 3 Sep 2008 API stability, decoupled admin, unicode

1.1 29 Jul 2009 Aggregates, transaction based tests

Multiple db connections, CSRF, model


1.2 17 May 2010
validation
Timezones, in browser testing, app
1.3 23 Mar 2011
templates.

1.5 26 Feb 2013 Python 3 Support, configurable user model

Dedicated to Malcolm Tredinnick, db


1.6 6 Nov 2013 transaction management, connection
pooling. 6
Migrations, application loading and
1.7 2 Sep 2014
configuration.

Migrations, application loading and


1.8 LTS 2 Sep 2014
configuration.

Native support for multiple template


1.8 LTS 1 Apr 2015
engines.Supported until at least April 2018

Automatic password validation. New styling


1.9 1 Dec 2015
for admin interface.

Full text search for PostgreSQL. New-style


1.10 1 Aug 2016
middleware.

Last version to support Python 2.7.Supported


1.11 LTS 1.11 LTS
until at least April 2020

First Python 3-only release, Simplified URL


2.0 Dec 2017
routing syntax, Mobile friendly admin.
7
Features of Django

Versatile
Rapid Development

Scalable Open Source

Vast and Supported


Secure Community

8
Django Installation

To install Django, first visit to django official site


(https://www.djangoproject.com) and download django by
clicking on the download section. Here, we will see various
options to download The Django.

Django requires pip to start installation. Pip is a package


manager system which is used to install and manage
packages written in python. For Python 3.4 and higher
versions pip3 is used to manage packages.
9
Django Installation

In this tutorial, we are installing Django in


Ubuntu operating system.

The complete installation process is described below.


Before installing make sure pip is installed in local system.

Here, we are installing Django using pip3, the installation


command is given below.

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

In the previous topic, we have installed Django successfully.


Now, we will learn step by step process to create a Django
application.

13
Django Project Example
Here, we are creating a project djangpapp in the current directory.

$ django-admin startproject djangpapp

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

Django project has a built-in development server which is


used to run application instantly without any external web
server. It means we don't need of Apache or another web
server to run the application in development mode.

To run the application, we can use the following command.

$ python3 manage.py runserver

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

01 What is Model 02 Create First Model

03 Model Fields 04 Databases

21
What is Model?
❑ A model is the single, definitive source of information about
your data.

❑ It contains the essential fields and behaviors of the data


you’re storing

❑ Generally, each model maps to a single database table.

❑ Each model is a Python class that subclasses


django.db.models.Model.

❑ Each attribute of the model represents a database field.

22
CREATE YOUR FIRST MODEL

from django.db import models

class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)

23
MODEL FIELDS

Fields are organized into records, which contain all the


information within the table relevant to a specific entity.

There are concepts to know before creating 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.

AutoField() BooleanField() CharField()

An integer field Store true/false A string field


that value and for small to
automatically generally used large-sized
increments for checkboxes strings.

DateField()

A date field
represents
python
datetime. date
instance. 25
2. FIELD OPTIONS

Field option are used to customize and put constraint on the


table rows.

E.g.

name= models.CharField(max_length = 60)

here "max_length" specifies the size of the VARCHAR field.

26
The following are some common and mostly used field option:

01 Null
02 Blank

to store empty if True, the field


values as NULL in s allowed to be
database. 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

The power of relational databases lies in relating tables to each


other Django offers ways to define the three most common
types of database relationships:

1. many-to-one
2. many-to-many
3. one-to-one.

28
1) Many-to-one relationships:

To define a many-to-one relationship, use


django.db.models.ForeignKey.
You use it just like any other Field type: by including it as a class
attribute of your model.

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

To define a many-to-many relationship, use ManyToManyField.


You use it just like any other Field type: by including it as a
class attribute of your model.

For example, if a Pizza has multiple Topping objects – that is, a


Topping can be on multiple pizzas and each Pizza has multiple
toppings – here’s how you’d represent that:

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

To define a one-to-one relationship, use OneToOneField. You


use it just like any other Field type: by including it as a class
attribute of your model.

E.g.

from django.conf import settings


from django.db import models

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.

❑ A class defines how an instance of the class behaves while


a metaclass defines how a class behaves.

❑ A class is an instance of a metaclass.

❑ Give your model metadata by using an inner class Meta.

33
E.g.

from django.db import models

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

Before we can create any models, we must first setup our


database configuration. To do this, open the settings.py and
locate the dictionary called DATABASES.

modify the default key/value pair so it looks something like


the following example.

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

DATABASE_PATH = os.path.join(PROJECT_PATH, 'rango.db')

37

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy