Python學習之路—2018/6/26

來源:互聯網
上載者:User

標籤:bubuko   inf   from   djang   app   date   name   欄位   S3   

Python學習之路—2018/6/261.ORM單表操作刪除與修改記錄
>>> ret = Book.objects.filter(title="go").delete()(1, {‘app01.Book‘: 1})>>> Book.objects.filter(price=115).update(price=120)

多表操作建立模型

一對一

models.OneToOneField(to="表名", on_delete=models.CASCADE)

一對多

models.ForeignKey(to="表名", to_field="欄位名", on_delete=models.CASCADE)

多對多

models.ManyToManyField(to="表名")

models.py

from django.db import modelsclass Author(models.Model):    nid = models.AutoField(primary_key=True)    name = models.CharField(max_length=32)    age = models.IntegerField()    # 與AuthorDetail建立一對一關聯性    author_detail = models.OneToOneField(to="AuthorDetail", on_delete=models.CASCADE)class AuthorDetail(models.Model):    aid = models.AutoField(primary_key=True)    birthday = models.DateField()    telephone = models.BigIntegerField()    addr = models.CharField(max_length=64)class Publish(models.Model):    pid = models.AutoField(primary_key=True)    name = models.CharField(max_length=32)    city = models.CharField(max_length=32)    email = models.EmailField()class Book(models.Model):    bid = models.AutoField(primary_key=True)    title = models.CharField(max_length=32)    publishDate = models.DateField()    price = models.DecimalField(max_digits=5, decimal_places=2)    # 與Publish建立一對多關聯性,外鍵欄位建立在多的一方    publish = models.ForeignKey(to="Publish", to_field="pid", on_delete=models.CASCADE)    """        publish_id INT,        FOREIGN KEY(publish_id) REFERENCES publish(pid)    """    # 與Author建立多對多關係    authors = models.ManyToManyField(to="Author")    """        CREATE TABLE book_author(            id INT PRIMARY KEY AUTO_INCREMENT,            book_id INT,            author_id INT,            FOREIGN KEY(book_id) REFERENCES Book(bid),            FOREIGN KEY(author_id) REFERENCES Author(nid),        )    """

運行結果:

注意:

  • on_delete = models.CASCADE在使用一對以及一對多時需要用到
  • 外鍵欄位會自動添加_id,比如publish = models.ForeignKey()執行後產生publish_id
  • 需要在settings.py中配置app01
添加記錄

一對多

Book.objects.create(title="壞蛋是怎樣煉成的", publishDate="2017-01-02", price=125, publish_id=1)

運行結果:

>>> book = Book.objects.get(bid=1)>>> print(book.bid)1>>>print(book.title)壞蛋是怎樣煉成的>>> print(book.publishDate)2017-01-02>>> print(book.price)125.00>>> print(book.publish)Publish object (1)>>> print(book.publish.name)中國城市出版社>>> print(book.publish.email)[email protected]

Python學習之路—2018/6/26

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.