Django model 练习

 

 

# 作业
class Author(models.Model):
    '''
    作者
    '''
    name = models.CharField(max_length=100)
    age = models.IntegerField()

class BookType(models.Model):
    '''
    图书
    '''
    caption = models.CharField(max_length=64)

class Book(models.Model):
    name = models.CharField(max_length=64)
    pages = models.IntegerField()
    price = models.DecimalField(max_digits=10,decimal_places=2)
    pubdate = models.DateField(auto_now=True)

    authors = models.ManyToManyField("Author")
    book_type = models.ForeignKey("BookType")

  

 

 

#####作业
    #1 添加数据
    # BookType.objects.create(caption="文学")
    # BookType.objects.create(caption="地理")
    # BookType.objects.create(caption="天文")
    # Author.objects.create(name="alex",age=18)
    # Author.objects.create(name="eric",age=19)
    # Author.objects.create(name="liujianzuo",age=20)
    # 2  等1 创建完在创建这个
    # Book.objects.create(name="金品梅",pages=11111,price=1,book_type_id=1)
    # Book.objects.create(name="某某传",pages=11112,price=3,book_type_id=1)
    # Book.objects.create(name="某传",pages=11113,price=2,book_type_id=1)
    # print("ok")

    # 3 添加多对多关系表 通过alex反射三本书  外键字段在book类 因此反向插入
    # auth = Author.objects.get(name="alex")
    # auth.book_set.add(*Book.objects.filter(id__gt=1)) # filter 获取的是个集合 内部是迭代的对象
    # auth.book_set.add(*Book.objects.filter(id=1)) #如果这里用get而不是filter就不用加*

    # 3 正向插入
    # from django.db.models import Q
    # buk = Book.objects.get(id =1)
    # buk.authors.add(*Author.objects.filter(~Q(name ="alex"))) # filter过滤不等于

    # 4 通过根据书名获取到对象,外键在这个book类,因此需要正向查找
    # 4.1 正向第一种
    # ret = Book.objects.filter(book_type__caption="文学").values("name","price","pubdate","book_type")
    # print(ret)
    # for k in ret:
    #     print("==bookname--%s==="%(k['name']))
    #     for n,v in k.items():
    #         print(n,v)


    # 4.2 正向第二种查找
    # ret = Book.objects.all()
    # for item in ret:
    #     if item.book_type.caption == "文学":
    #         print(item.name,item.price,item.pubdate,item.book_type.caption)


    #4.3 反向查找 第一种 book_set
    # obj = BookType.objects.get(caption="文学")
    # ret = obj.book_set.all()
    # for item in ret:
    #     print(item.name,item.price,item.pubdate,item.book_type.caption)# 非filter跨表用点 item.book_type.caption

    # 4.4 反向查找 第二种
    # ret = BookType.objects.all().values("book__name","book__price","book__pubdate","caption")
    # for item in ret:
    #     if item["caption"] == "文学":
    #         for k,v in item.items():
    #             print(k,v)
    #     else:
    #         continue

    # 5 查找作者alex 参与的所有书籍,等。。 多对多 外键在book类 ,从book类查找是正向

    # 5.1 第一种查找
    # ret = Book.objects.filter(authors__name="alex").values("name","price","pubdate","authors__name","book_type__caption")
    # for k in  ret:
    #     for n,v in k.items():
    #         print(n,v)

    # 5.2 第二种
    # ret= Book.objects.all()
    # for item in ret:
    #     # print(item.authors.all().values("book__authors__name"))
    #     x = item.authors.all().values("book__authors__name")
    #     for n in x:
    #         for k,v in n.items():
    #             if v == "alex" :
    #                 print(v, item.name, item.price, item.pubdate, item.book_type.caption)

    # 5.3 第三种
    # ret = Author.objects.get(name="alex") # get获取
    # obj = ret.book_set.all()
    # print(obj)
    # for i in obj:
    #     print(ret.name,i.name,i.price,i.pubdate,i.book_type.caption)

    # 5.4 第四种

    # ret = Author.objects.all().values("name","book__name","book__price","book__pubdate","book__book_type__caption")
    # # print(ret)
    # for item in ret:
    #     if item["name"] == "alex":
    #         for k,v in item.items():
    #             print(k,v)
    #     else:
    #         continue

  

 

posted @ 2016-08-23 21:42  众里寻,阑珊处  阅读(190)  评论(0编辑  收藏  举报
返回顶部