Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Two Scoops of Django / Model

tim
November 28, 2013
300

Two Scoops of Django / Model

this is my presentation about two scoops of django's chapter 6

tim

November 28, 2013
Tweet

Transcript

  1. 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
  2. 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
  3. Basic • How much Model in an app is appropriate

    • Can I write raw SQL? • Can I add index?
  4. 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
  5. 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()
  6. 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
  7. South Issue • After you finish your first version of

    django app • python manage.py schemamigration app — initial • add migration test if possible
  8. Concern Cache • Concern Cache first. than the De-normalize •

    CH20 discuss more about reducing bottle neck
  9. 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)
  10. Summary • Start normalized • Don’t forget to use indexes.

    • Watch out for the “gotchas” when using the null=True and blank=True • model inheritance