Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Two Scoops of Django / Model
Search
tim
November 28, 2013
390
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Two Scoops of Django / Model
this is my presentation about two scoops of django's chapter 6
tim
November 28, 2013
Transcript
22 slides
django model
Recap class SafeUser(models.Model): ! user = models.OneToOneField(User,related_name='safe_user') display_name = models.CharField(null=True,
blank=True, max_length=1024) ! def __unicode__(self): return self.user.username + ' ' + self.display_name
Outline • Good Packages • Model Inheritance • South Experiences
• secret behind fields’ parameter
recommended package • south • django-model-util • django extension
South • Existing Database need another command to alter the
table • South do it for you. • https://docs.djangoproject.com/en/dev/releases/ 1.7/ <— syncdb will be deprecated
shell_plus • command from djagno extension
django-model-util • STATUS model • Field Tracker ( previous value
of your model)
Basic • How much Model in an app is appropriate
• Can I write raw SQL? • Can I add index?
Model Inheritance
Situations of Inheritance • Share Common Parts • Reuse Table
• Add Extra Ability
share Common Part ! • Base Class will not be
a table • share common fields between table class CommonInfo(models.Model): name = models.CharField(max_length=100) age = models.PositiveIntegerField() ! class Meta: abstract = True
multi-table inheritance • Just inherit • will create two table
• implicitly using foreign key from django.db import models ! class Place(models.Model): name = models.CharField(max_length=50) address = models.CharField(max_length=80) ! class Restaurant(Place): serves_hot_dogs = models.BooleanField() serves_pizza = models.BooleanField()
Proxy Model • No Extra Table • add more function
class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) ! class MyPerson(Person): class Meta: proxy = True ! def do_something(self): # ... pass
South Issue • After you finish your first version of
django app • python manage.py schemamigration app — initial • add migration test if possible
normalize? • denormalized carefully
Concern Cache • Concern Cache first. than the De-normalize •
CH20 discuss more about reducing bottle neck
Fields • Blank is logical, for validation • null is
related to how database save empty value • any field without null=True specified in the model needs to have a value set (from Malcolm)
None
Manager • Looking very good • More Readable
You have two choice
the reason • First manager will be used by django
Summary • Start normalized • Don’t forget to use indexes.
• Watch out for the “gotchas” when using the null=True and blank=True • model inheritance
More Decks by tim
See All by tim
meta programing with django orm
timtan
1
56
Transaction_Behavior_in_Django.pdf
timtan
0
41
Featured
See All Featured
Side Projects
sachag
456
43k
Visualization
eitanlees
153
17k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
121
130k
WENDY [Excerpt]
tessaabrams
15
41k
Heart Work Chapter 1 - Part 1
lfama
PRO
11
37k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
680
Odyssey Design
rkendrick25
PRO
3
970
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
370
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.7k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k