21. Does not care extra DB table columns
Does not care extra DB tables
Complains if model contains fields that
has not yet been created in the DB table
Django
2013年7月24日星期三
25. Add model fields
Add fields to the model
Check the column definitions for new fields
Add new columns to the DB table
Verify new fields was added properly
In development environments ...
2013年7月24日星期三
26. Add model fields
Add fields to the model
Check the column definitions for new fields
Add new columns to the DB table
Verify new fields was added properly
In development environments ...
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
num_pages = models.IntegerField(blank=True, null=True)
def __unicode__(self):
return self.title
2013年7月24日星期三
27. Add model fields
Add fields to the model
Check the column definitions for new fields
Add new columns to the DB table
Verify new fields was added properly
In development environments ...
CREATE TABLE "books_book" (
"id" serial NOT NULL PRIMARY KEY,
"title" varchar(100) NOT NULL,
"publisher_id" integer NOT NULL REFERENCES "books_publisher" ("id"),
"publication_date" date NOT NULL,
"num_pages" integer NULL
);
$ python manage.py sqlall books
2013年7月24日星期三
28. Add model fields
Add fields to the model
Check the column definitions for new fields
Add new columns to the DB table
Verify new fields was added properly
In development environments ...
ALTER TABLE books_book ADD COLUMN num_pages integer;
2013年7月24日星期三
29. Add model fields
Add fields to the model
Check the column definitions for new fields
Add new columns to the DB table
Verify new fields was added properly
In development environments ...
>>> from mysite.books.models import Book
>>> Book.objects.all()[:5]
2013年7月24日星期三
30. Add model fields
Add new columns to the DB table
Add fields to the model
Restart the web server
In production environments ...
2013年7月24日星期三
32. Add model fields
Remove fields from the model
Restart the web server
Remove columns from the DB table
Remove Normal fields ...
2013年7月24日星期三
33. Add model fields
Remove fields from the model
Restart the web server
Remove columns from the DB table
Remove Normal fields ...
ALTER TABLE books_book DROP COLUMN num_pages;
2013年7月24日星期三
34. Add model fields
Remove Many-to-Many fields from the model
Restart the web server
Remove Many-to-Many table from the DB
Remove Many-to-Many fields ...
2013年7月24日星期三
35. Add model fields
Remove Many-to-Many fields from the model
Restart the web server
Remove Many-to-Many table from the DB
Remove Many-to-Many fields ...
DROP TABLE books_book_authors;
2013年7月24日星期三
37. Remove model from the models.py
Restart the web server
Remove dependent tables from the DB
Remove the target table from the DB
Remove models
2013年7月24日星期三
38. Remove model from the `models.py`
Restart the web server
Remove dependent tables from the DB
Remove the target table from the DB
Remove models
DROP TABLE books_book;
2013年7月24日星期三
53. Add extra Manager methods
# Get the number of books that have a title ‘Django’
>>> Book.objects.filter(title__icontains='Django')
# Get the number of books that have a title ‘Python’
>>> Book.objects.filter(title__icontains='Python')
# Get the number of books that have a title ‘xxx’
....
# Get the number of books that have a title ‘yyy’
....
.....
.......
2013年7月24日星期三
54. Add extra Manager methods
# takes a keyword and returns the number of books
>>> Book.objects.title_count('django')
>>> Book.objects.title_count('python')
2013年7月24日星期三
55. # models.py
from django.db import models
# ... Author and Publisher models here ...
class BookManager(models.Manager):
def title_count(self, keyword):
return self.filter(title__icontains=keyword).count()
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
num_pages = models.IntegerField(blank=True, null=True)
objects = BookManager()
def __unicode__(self):
return self.title
2013年7月24日星期三
56. # models.py
from django.db import models
# ... Author and Publisher models here ...
class BookManager(models.Manager):
def title_count(self, keyword):
return self.filter(title__icontains=keyword).count()
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
num_pages = models.IntegerField(blank=True, null=True)
objects = BookManager()
def __unicode__(self):
return self.title
2013年7月24日星期三
58. Modify initial Manager QuerySets
Default Manager return all the records
Override Manager’s base QuerySets
2013年7月24日星期三
59. Modify initial Manager QuerySets
Default Manager return all the records
Override Manager’s base QuerySets
# returns all books in the book database
>>> Book.objects.all()
2013年7月24日星期三
60. Modify initial Manager QuerySets
Default Manager return all the records
Override Manager’s base QuerySets
By overriding the Manager.get_query_set()
2013年7月24日星期三
61. Modify initial Manager QuerySets
from django.db import models
# First, define the Manager subclass.
class DahlBookManager(models.Manager):
def get_query_set(self):
return super(DahlBookManager, self).get_query_set()
.filter(author='Roald Dahl')
# Then hook it into the Book model explicitly.
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=50)
# ...
objects = models.Manager() # The default manager.
dahl_objects = DahlBookManager() # The Dahl-specific manager.
2013年7月24日星期三
62. Modify initial Manager QuerySets
from django.db import models
# First, define the Manager subclass.
class DahlBookManager(models.Manager):
def get_query_set(self):
return super(DahlBookManager, self).get_query_set()
.filter(author='Roald Dahl')
# Then hook it into the Book model explicitly.
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=50)
# ...
objects = models.Manager() # The default manager.
dahl_objects = DahlBookManager() # The Dahl-specific manager.
2013年7月24日星期三
64. Another example
class MaleManager(models.Manager):
def get_query_set(self):
return super(MaleManager, self).get_query_set().filter(sex='M')
class FemaleManager(models.Manager):
def get_query_set(self):
return super(FemaleManager, self).get_query_set().filter(sex='F')
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
sex = models.CharField(max_length=1, choices=(('M', 'Male'), ('F', 'Female')))
people = models.Manager()
men = MaleManager()
women = FemaleManager()
2013年7月24日星期三
65. Another example
class MaleManager(models.Manager):
def get_query_set(self):
return super(MaleManager, self).get_query_set().filter(sex='M')
class FemaleManager(models.Manager):
def get_query_set(self):
return super(FemaleManager, self).get_query_set().filter(sex='F')
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
sex = models.CharField(max_length=1, choices=(('M', 'Male'), ('F', 'Female')))
people = models.Manager()
men = MaleManager()
women = FemaleManager()
2013年7月24日星期三