Download - An Overview of Models in Django
![Page 1: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/1.jpg)
An Overview of Models in Django
![Page 2: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/2.jpg)
Michael Auritt
• Director of Media Production at CorpU
• Self-taught programmer
• Twitter: @mauritt
![Page 4: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/4.jpg)
Django
• Models• Views• Templates
![Page 5: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/5.jpg)
Django
• Models - Manage our data
• Views - Query the data
• Template - Present the data
![Page 6: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/6.jpg)
Django
• Models - Manage our data
• Views - Query the data
• Template - Present the data
![Page 7: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/7.jpg)
Models
• The single definitive source of information about your website data
• Uses Django’s Object-relational Mapper
• Each model maps to a single database table
• Each field maps to a column within that table
![Page 8: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/8.jpg)
Talent Management The Happiness Advantage
CorpU Promo
My Video PortfolioVideos | About | Contact
Supply Chain Management
http://www.myvideoportfolio.com/videos
![Page 9: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/9.jpg)
Supply Chain Management
My Video PortfolioVideos| About | Contact
http://www.djangodjunction.com/djangos/01
This course explores end-to-end supply chain management. This video takes a look at the main elements of a modern supply chain and…
![Page 10: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/10.jpg)
Supply Chain Management
My Video PortfolioVideos| About | Contact
http://www.djangodjunction.com/djangos/01
TITLE
DESCRIPTION
Embed
This course explores end-to-end supply chain management. This video takes a look at the main elements of a modern supply chain and…
![Page 11: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/11.jpg)
models.py
from django.db import models
class Video(models.Model):
title = models.CharField(max_length = 200) description = models.TextField() embed_key = models.IntegerField() date_completed = models.DateField()
![Page 12: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/12.jpg)
id(primary key) title descriptio
n embed_key date_completed
Video
![Page 13: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/13.jpg)
id title description embed_key date_completed
1 Ursa Motors In this video… 679210 2014-10-01
2 Sea BoxSea Box is a
company that…
933750 2015-03-15
3Critical
ThinkingIn Critical Thinking… 876025 2014-09-23
4 CorpU Promo CorpU’s platform… 867302 2015-02-05
video
![Page 14: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/14.jpg)
models.py
from django.db import models
class Video(models.Model):
title = models.CharField(max_length = 200) description = models.TextField() embed_key = models.IntegerField() date_completed = models.DateField()
![Page 16: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/16.jpg)
models.py
from django.db import models
class Video(models.Model):
title = models.CharField(max_length = 200) description = models.TextField() embed_key = models.IntegerField() date_completed = models.DateField()
![Page 17: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/17.jpg)
title = models.CharField(max_length = 200)
![Page 18: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/18.jpg)
hidden = models.BooleanField(default = True)
![Page 19: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/19.jpg)
slug = models.SlugField(unique = True)
![Page 20: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/20.jpg)
Validators
• validate_slug
• EmailValidator
• URLValidator
![Page 21: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/21.jpg)
Validators
(validate = [email_validator])
![Page 22: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/22.jpg)
models.pyfrom django.db import modelsfrom django.core.exceptions import ValidationError
def validate_embed(value): valid_embed = (checks embed at vimeo.com) if not valid_embed: raise ValidationError(‘That embed code does not exist at vimeo.com’)
class Video(models.Model): embed = integerField(validators = [validate_embed])
![Page 23: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/23.jpg)
Field ChoicesGENRE_CHOICES = ( (‘DOC’, ‘Documentary’ ), (‘PRO’, ‘Promo’), (‘NAR’,’Narrative’),)
genre = models.CharField( max_length = 3, choices = GENRE_CHOICES)
![Page 24: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/24.jpg)
models.pyfrom django.db import models
class Video(models.Model):
title = models.CharField(max_length = 200) description = models.TextField() embed_key = models.IntegerField() date_completed = models.DateField() hidden = models.BooleanField(default = True) slug = models.SlugField(unique = True)
![Page 25: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/25.jpg)
Relationships
• Many to One• Many to Many• One to One
![Page 26: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/26.jpg)
Many to One
One row of a database table relates to many rows of another.
![Page 27: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/27.jpg)
title … client
… Client: 01
… Client: 02
… Client: 01
… Client: 03
… Client: 01
… Client: 02
id name
website …
01 … …
02 … …
03 …
VideoClient
Many to One
![Page 28: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/28.jpg)
Many to One
class Client(models.Model): name = models.CharField(unique = True) website = models.URLField()
class Video(models.Model): client = models.ForeignKey(Client)
![Page 29: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/29.jpg)
Many to Many
Many rows of a database table relate to many rows of another.
![Page 30: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/30.jpg)
… role
… role: Camera Op, Editor, Producer
… role: Camera Op, Editor
… role: Producer
… role: Editor
name …
Camera Op …
Editor …
Producer …
VideoRole
Many to Many
![Page 31: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/31.jpg)
Many to Many
class Role(models.Model): name = models.CharField(unique = True)
class Video(models.Model): role = models.ManyToManyField(Role)
![Page 32: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/32.jpg)
One to One
One row of a database table relates to one row of another.
![Page 33: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/33.jpg)
… file
… File: 1
… File: 2
… File 3
… FIle 4
VideoOne to One
id path
1
2
3
4
File
![Page 34: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/34.jpg)
One to One
class File(models.Model): drive = models.CharField() path = models.FilePathField()
class Video(models.Model): file = models.OneToOneField(File)
![Page 35: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/35.jpg)
$ python manage.py makemigrations
![Page 36: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/36.jpg)
Migrations operations = [ migrations.CreateModel( name='Video', fields=[ ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')), ('title', models.CharField(max_length=200)), ('description', models.TextField()), ('embed', models.TextField()), ('company', models.ForeignKey(to='videos.Company')), ('roles', models.ManyToManyField(to='videos.Role')),
![Page 37: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/37.jpg)
$ python manage.py migrate
![Page 38: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/38.jpg)
Migrations
operations = [ migrations.AlterField( model_name='company', name=‘slug’, field=models.SlugField(unique=True),
![Page 39: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/39.jpg)
Migration Issues
• Adding new fields without a default
• Adding new unique fields
• Changing relationships
![Page 40: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/40.jpg)
QueriesReturning Many Objects
• Video.objects.all()
• Video.obects.filter(client = ‘MasterCard’)
• Video.objects.all().order_by(‘date_created’)
![Page 41: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/41.jpg)
QueriesReturning Many Objects
MC_Videos = Video.obects.filter(client = ‘MasterCard’)
MC_Ascending_Order = MC_videos.order_by(‘title’)
MC_Descending_Order = MC_videos.order_by(‘-title’)
![Page 42: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/42.jpg)
QueriesReturning Many Objects
MC_videos = Video.obects.filter(client = ‘MasterCard’)
MC_videos = MC_videos.order_by(‘date_created’)
MC_videos = MC_videos.exclude(Role = ‘Producer’)
![Page 43: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/43.jpg)
Field Lookupsfield__lookuptype=value
.filter(title__startswith=‘G’)
.filter(date_completed__gte=datetime(2015,01,01))
.filter(title__contains = ‘Supply Chain’)
![Page 44: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/44.jpg)
QueriesReturning Single Objects
• Video.objects.get(pk = 1)
• Video.objects.get(title = ‘My Best Video’)
![Page 45: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/45.jpg)
Queries
• QuerySet API Reference in Django Docs
• QuerySets are lazy
• QuerySets are faster than using Python
![Page 46: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/46.jpg)
Queries>>>from video.models import Video
>>> v = Video.objects.get(pk = 1)
>>> print(v)
<Video: Video object>
![Page 47: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/47.jpg)
Model Methodsfrom django.db import models
class Video(models.Model):
title = models.CharField(max_length = 200) def __str__(self): return self.title
![Page 48: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/48.jpg)
Queries>>>from video.models import Video
>>> v = Video.objects.get(pk = 1)
>>> print(v)
<Video: Talent Management>
![Page 49: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/49.jpg)
Model Methodsclass Role(models.Model): name = models.CharField(unique = True)
class Video(models.Model): def is_full_stack(self): full_stack = True for role in Role.objects.all(): if role not in self.role.all(): full_stack = False return full_stack
![Page 50: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/50.jpg)
Model Methods>>>from video.models import Video
>>> v = Video.get(pk = 1)
>>> v.is_full_stack()
True
![Page 51: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/51.jpg)
Views.py
def detail(request, slug): try: video = Video.Objects.get(slug = slug) except video.DoesNotExist: raise Http404() else: context = {‘video’: video} return render(request, ‘videos/detail.html’, context)
![Page 52: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/52.jpg)
Model Manager
video.objects.all()
![Page 53: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/53.jpg)
Model Manager
video.objects.all()
models.Manger
![Page 54: An Overview of Models in Django](https://reader036.vdocument.in/reader036/viewer/2022062523/58f243c71a28ab815f8b458f/html5/thumbnails/54.jpg)
Thanks
Further Reading
• djangoproject.com
• Two Scoops of Django (TwoScoopsPress.com)
• Lightweight Django (O’Reilly)