最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过。Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧~
本篇笔记(其实我的所有笔记都是),并不会过于详细的讲解。因此如果有大家看不明白的地方,欢迎在我正版博客下留言,有时间的时候我很愿意来这里与大家探讨问题。(当然,不能是简简单单就可以百度到的问题-.-)
我所选用的教材是《The Django Book 2.0》,本节是第十章,模型高级进阶。
在基础部分的学习中,我体会到了一点经验:傻瓜教程最适合作为本书的笔记了~因为本书对于原理讲得很细,看一遍也就能基本理解,但由于讲得太细,具体操作步骤正是其不足。因此,读这本书,如果配上操作教程式的笔记,那复习起来就很舒服了 ^.^
因此,高级部分的笔记,将给出很多操作教程,没看过书的同学请先看了书再来看笔记~
0. 目录
1. 模型回顾与初探
模型代码如下:
from django.db import models class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField() def __unicode__(self):
return self.name class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField() def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name) class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField() def __unicode__(self):
return self.title
1.1 访问外键(Foreign Key)值
Book的外键是Publisher,首先,访问Book代码如下:
# Book >>> from mysite.books.models import Book
>>> b = Book.objects.get(id=50)
>>> b.title
u'The Django Book'
然后,通过Book访问Publisher代码如下:
# Publisher >>> b = Book.objects.get(id=50)
>>> b.publisher
<Publisher: Apress Publishing>
>>> b.publisher.website
u'http://www.apress.com/'
通过Publisher访问Book的代码则如下:
# Book1 >>> p = Publisher.objects.get(name='Apress Publishing')
>>> p.book_set.all()
[<Book: The Django Book>, <Book: Dive Into Python>, ...] # Book2 >>> p = Publisher.objects.get(name='Apress Publishing')
>>> p.book_set.filter(name__icontains='django')
[<Book: The Django Book>, <Book: Pro Django>]
1.2 访问多对多(Many-to-Many)值
通过Book访问Author代码如下:
# Author >>> b = Book.objects.get(id=50)
>>> b.authors.all()
[<Author: Adrian Holovaty>, <Author: Jacob Kaplan-Moss>]
>>> b.authors.filter(first_name='Adrian')
[<Author: Adrian Holovaty>]
>>> b.authors.filter(first_name='Adam')
[]
通过Author访问Book代码则如下:
# Book >>> a = Author.objects.get(first_name='Adrian', last_name='Holovaty')
>>> a.book_set.all()
[<Book: The Django Book>, <Book: Adrian's Other Book>]
2. 更改数据库模式(Database Schema)
当你修改模型时(例如添加字段、删除字段、删除模型……),应当依次进行以下步骤:
(1) 修改models.py中模型
(1+) 这时你的admin页面依旧正常运行,查看时也的确是修改完毕的样子,但是一旦添加(修改)对象则会报错
(2) 生成migration python manage.py makemigrations appname
(3) 激活模型(migrate) python manage.py migrate
(3+) 现在你可以正常使用了 ^.^
上述过程肯定是没问题的,除非你修改的是外键,那么就会很麻烦了。
例如,你删除了Book模型的外键,再要恢复则会在第2步遇到这样的提示:
You are trying to add a non-nullable field 'publisher' to book without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option:
我不知道应该如何给值,于是选的2,结果……再也无法打开Book了……报错信息如下:
OperationalError at /admin/books/book/
(1054, "Unknown column 'books_book.publisher_id' in 'field list'")
因此,在我知道如何做之前,权宜之计,我不会尝试修改外键!
3. Managers
我们之前一直在使用的Book.objects.all()之类的语句,其Book.objects就是所谓的Manager,这是Django定义的用来管理模型的类,其中定义了很多函数,例如.all()。
而其中各种函数,例如all()、get(),返回的有类似列表的QuerySet,也有单个对象。
3.1 增加额外Manager的方法
有时候objects的功能不够用,我们就得写自己的Manager,代码如下:
# 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
上述代码的第7~9行定义了自己的Manager,而第17行则把objects设为了这个Manager。
如此一来,我们便可以做如下操作了:
>>> Book.objects.title_count('django')
4
>>> Book.objects.title_count('python')
18
如果我们不定义objects,那么Django会自动生成;而现在咱们重新定义了objects,那么它就是咱们定义的这个Manager了。
多说一句,上面代码的作用,是查找所有书籍中名字带有指定字符串的书籍数量。
3.2 修改初始Manager QuerySets
我们在写出Book.objects()的时候,Django会认为我们选中了所有的Book对象,这是因为默认的objects类中有一个函数:get_query_set(),这个函数决定了你调用objects的时候选中哪些对象,它默认是返回模型中所有对象的。
那么,我们便可以改写一下这个函数,来让我们选中的对象(Query Set)改变一下,代码如下:
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.
如此一来,下面的Book.dahl_objects.all()便是上面函数所选的对象了:作者是Roald Dahol的书。
Book.dahl_objects.all()
Book.dahl_objects.filter(title='Matilda')
Book.dahl_objects.count()
再比如,我们可以在一个模型中实现几个不同的Manager,代码如下:
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()
4. 模型方法
我们之前一直在使用模型自带的方法,现在介绍如何自己定义方法:
from django.contrib.localflavor.us.models import USStateField
from django.db import models class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
birth_date = models.DateField()
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)
state = USStateField() # Yes, this is U.S.-centric... def baby_boomer_status(self):
"Returns the person's baby-boomer status."
import datetime
if datetime.date(1945, 8, 1) <= self.birth_date <= datetime.date(1964, 12, 31):
return "Baby boomer"
if self.birth_date < datetime.date(1945, 8, 1):
return "Pre-boomer"
return "Post-boomer" def is_midwestern(self):
"Returns True if this person is from the Midwest."
return self.state in ('IL', 'WI', 'MI', 'IN', 'OH', 'IA', 'MO') def _get_full_name(self):
"Returns the person's full name."
return u'%s %s' % (self.first_name, self.last_name)
full_name = property(_get_full_name)
之后,使用代码如下:
>>> p = Person.objects.get(first_name='Barack', last_name='Obama')
>>> p.birth_date
datetime.date(1961, 8, 4)
>>> p.baby_boomer_status()
'Baby boomer'
>>> p.is_midwestern()
True
>>> p.full_name # Note this isn't a method -- it's treated as an attribute
u'Barack Obama'
5. 执行原始SQL查询
最后,有时候会发现Django定义的内容不全,我们需要写一些sql代码,这时候就要:
>>> from django.db import connection
>>> cursor = connection.cursor()
>>> cursor.execute("""
... SELECT DISTINCT first_name
... FROM people_person
... WHERE last_name = %s""", ['Lennon'])
>>> row = cursor.fetchone()
>>> print row
['John']
实际写入代码中的时候,最好写在自己的Manager中,就像这样:
from django.db import connection, models class PersonManager(models.Manager):
def first_names(self, last_name):
cursor = connection.cursor()
cursor.execute("""
SELECT DISTINCT first_name
FROM people_person
WHERE last_name = %s""", [last_name])
return [row[0] for row in cursor.fetchone()] class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
objects = PersonManager()
如此,便可这样使用:
>>> Person.objects.first_names('Lennon')
['John', 'Cynthia']
至此,“模板高级进阶”内容完结,下一篇是——“通用视图”。