Django, one-to-many, many-to-many


1、定義關系

  • 定義三個表,Publisher,Book,Author

  • 一個作者有姓,有名及email地址。
  • 出版商有名稱,地址,所在城市、省,國家,網站。

  • 書籍有書名和出版日期。 它有一個或多個作者(和作者是多對多的關聯關系[many-to-many]), 只有一個出版商(和出版商是一對多的關聯關系[one-to-many],也被稱作外鍵[foreign key])

2、定義model

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() class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField() class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField()

3、基本數據庫訪問

  當你使用Django modle API創建對象時Django並未將對象保存至數據庫內,除非你調用save() 方法

>>> from books.models import Publisher
>>> p1 = Publisher(name='Apress', address='2855 Telegraph Avenue', ... city='Berkeley', state_province='CA', country='U.S.A.', ... website='http://www.apress.com/') >>> p1.save() >>> p2 = Publisher(name="O'Reilly", address='10 Fawcett St.', ... city='Cambridge', state_province='MA', country='U.S.A.', ... website='http://www.oreilly.com/') >>> p2.save() >>> publisher_list = Publisher.objects.all() >>> publisher_list [, ]

  如果需要一步完成對象的創建與存儲至數據庫,就使用objects.create() 方法

>>> p1 = Publisher.objects.create(name='Apress',
...     address='2855 Telegraph Avenue',
...     city='Berkeley', state_province='CA', country='U.S.A.',
...     website='http://www.apress.com/')
>>> p2 = Publisher.objects.create(name="O'Reilly",
...     address='10 Fawcett St.', city='Cambridge',
...     state_province='MA', country='U.S.A.',
...     website='http://www.oreilly.com/')
>>> publisher_list = Publisher.objects.all()
>>> publisher_list

  數據過濾

>>> Publisher.objects.filter(name='Apress')
[<Publisher: Apress>]
>>> Publisher.objects.filter(country="U.S.A.", state_province="CA")
[<Publisher: Apress>]
>>> Publisher.objects.filter(name__contains="press")
[<Publisher: Apress>]

  contains部分會被Django翻譯成LIKE語句

 

  獲取單個對象

>>> Publisher.objects.get(name="Apress")
<Publisher: Apress>

  排序

>>> Publisher.objects.order_by("name")
[<Publisher: Apress>, <Publisher: O'Reilly>]

  訪問外鍵

>>> b = Book.objects.get(id=50)
>>> b.publisher >>> b.publisher.website u'http://www.apress.com/'

  訪問多對多

>>> 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_set

>>> a = Author.objects.get(first_name='Adrian', last_name='Holovaty')
>>> a.book_set.all() [<Book: The Django Book>, <Book: Adrian's Other Book>]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM