ORM创建数据库表的方式一

from django.shortcuts import renderfrom app01 import models# Create your views here.#添加对象# create方式一def data_opr(req):    pub=models.Publish.objects.filter(id=1)    authors=models.Author.objects.filter(id_gt=1)    #加models就对了    models.Book.objects.create(        title=‘漂流记‘,        price=1,        color=‘red‘,        publisher_id=4,    )    book=models.Book.objects.filter(id=2).values(‘price‘)[{‘price‘:45}]    authors=models.Author.objects.order_by(‘-id‘)    authors=models.Author.objects.filter(id=1)[0]    author1=models.Author.objects.get(id=3)    author2=models.Author.objects.get(id=4)    book = models.Book.objects.filter(id=3)[0]    book.author.add(author1,author2)    # 或者book.author.add(author1)    #     book.author.add(author2)    #或者  book.author.add(*[author1,author2])

orm

相关推荐