Slide 1

Slide 1 text

django model

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Outline • Good Packages • Model Inheritance • South Experiences • secret behind fields’ parameter

Slide 4

Slide 4 text

recommended package • south • django-model-util • django extension

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

shell_plus • command from djagno extension

Slide 7

Slide 7 text

django-model-util • STATUS model • Field Tracker ( previous value of your model)

Slide 8

Slide 8 text

Basic • How much Model in an app is appropriate • Can I write raw SQL? • Can I add index?

Slide 9

Slide 9 text

Model Inheritance

Slide 10

Slide 10 text

Situations of Inheritance • Share Common Parts • Reuse Table • Add Extra Ability

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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()

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

South Issue • After you finish your first version of django app • python manage.py schemamigration app — initial • add migration test if possible

Slide 15

Slide 15 text

normalize? • denormalized carefully

Slide 16

Slide 16 text

Concern Cache • Concern Cache first. than the De-normalize • CH20 discuss more about reducing bottle neck

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Manager • Looking very good • More Readable

Slide 20

Slide 20 text

You have two choice

Slide 21

Slide 21 text

the reason • First manager will be used by django

Slide 22

Slide 22 text

Summary • Start normalized • Don’t forget to use indexes. • Watch out for the “gotchas” when using the null=True and blank=True • model inheritance