20121030 The django book 笔记 数据模型高级进阶
访问外键值
1 >>> b = Book.objects.get(id=50) 2 >>> b.publisher 3 <Publisher: Apress Publishing> 4 >>> b.publisher.website 5 u'http://www.apress.com/' 6 7 8 >>> p = Publisher.objects.get(name='Apress Publishing') 9 >>> p.book_set.all() 10 [<Book: The Django Book>, <Book: Dive Into Python>, ...]
通过对象访问对应得外键,通过外键访问对应的外键的值p.book_set.all()
访问多对多值
1 >>> b = Book.objects.get(id=50) 2 >>> b.authors.all() 3 [<Author: Adrian Holovaty>, <Author: Jacob Kaplan-Moss>] 4 5 6 >>> a = Author.objects.get(first_name='Adrian', last_name='Holovaty') 7 >>> a.book_set.all() 8 [<Book: The Django Book>, <Book: Adrian's Other Book>]
正反查询

浙公网安备 33010602011771号