Django ORM訓練專題


圖書信息系統

表結構設計

#
class Book(models.Model):
    title = models.CharField(max_length=32)
    publish_date = models.DateField(auto_now_add=True)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    memo = models.TextField(null=True)
    # 創建外鍵,關聯publish
    publisher = models.ForeignKey(to="Publisher")
    # 創建多對多關聯author
    author = models.ManyToManyField(to="Author")

    def __str__(self):
        return self.title


# 出版社
class Publisher(models.Model):
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=32)

    def __str__(self):
        return self.name


# 作者
class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    phone = models.CharField(max_length=11)
    detail = models.OneToOneField(to="AuthorDetail")

    def __str__(self):
        return self.name


# 作者詳情
class AuthorDetail(models.Model):
    addr = models.CharField(max_length=64)
    email = models.EmailField()

 

練習題

查找所有書名里包含番茄的書
查找出版日期是2017年的書
查找出版日期是2017年的書名
查找價格大於10元的書
查找價格大於10元的書名和價格
查找memo字段是空的書

查找在北京的出版社
查找名字以沙河開頭的出版社

查找作者名字里面帶“小”字的作者
查找年齡大於30歲的作者
查找手機號是155開頭的作者
查找手機號是155開頭的作者的姓名和年齡

查找書名是“番茄物語”的書的出版社
查找書名是“番茄物語”的書的出版社所在的城市
查找書名是“番茄物語”的書的出版社的名稱

查找書名是“番茄物語”的書的所有作者
查找書名是“番茄物語”的書的作者的年齡
查找書名是“番茄物語”的書的作者的手機號碼

查找書名是“番茄物語”的書的作者的地址
查找書名是“番茄物語”的書的作者的郵箱

 


免責聲明!

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



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