Django ORM (二) 增加操作

数据库表结构生成完毕后,可以使用工具连接上去

在 app01_author 表创建基础记录

在 app01_publisher 表创建基础记录

添加 data_oper 方法

在 urls.py 文件上添加

from app01 import views

urlpatterns = [
 ....
 path('data_oper/', views.data_oper),
]

ORM 增加方法

create 方式一

在 views.py 文件上添加

from django.shortcuts import render, HttpResponse
from app01 import models
from  app01.models import Book,Author

def data_oper(req):
    Book.objects.create(
        title="hahaha",
        price=2,
        publication_date="2019-05-16",
        publisher_id=1,
    )

    return HttpResponse("Hello world")

浏览器访问 http://127.0.0.1:8000/data_oper/,成功生成一条记录

create 方式二

在 views.py 文件上修改

from django.shortcuts import render, HttpResponse
from app01 import models
from  app01.models import Book,Author

def data_oper(req):
    dic = { "title":"K8S", "price":1, "publication_date":"2019-05-02", "publisher_id":1 }
    Book.objects.create(**dic)

    return HttpResponse("Hello world")

浏览器访问 http://127.0.0.1:8000/data_oper/,生成一条记录

save 方式一

from django.shortcuts import render, HttpResponse
from app01 import models
from  app01.models import Book,Author

# Create your views here.

def data_oper(req):
    obj=Book(
        title="Python",
        price=99,
        publication_date="2019-05-06",
        publisher_id=1,
    )
    obj.save()

    return HttpResponse("Hello world")

save 方式二

from django.shortcuts import render, HttpResponse
from app01 import models
from  app01.models import Book,Author

# Create your views here.

def data_oper(req):
    obj=Book()
    obj.title="GO"
    obj.price=90
    obj. publication_date="2019-05-06"
    obj.publisher_id=1
    obj.save()

    return HttpResponse("Hello world")

外键一对多的(ForeignKey)

在 models.py 中 Book 表下定义外键为
publisher = models.ForeignKey(Publisher,on_delete=models.CASCADE)

方法一
在 views.py 文件上修改

from django.shortcuts import render, HttpResponse
from app01 import models
from  app01.models import Book,Author,Publisher

# Create your views here.

def data_oper(req):
    pub_obj = Publisher.objects.get(id=1)       # 取publish 表 id 为1 的值
    dic = {"title": "Docker", "price": 88, "publication_date": "2019-05-05", "publisher": pub_obj}
    Book.objects.create(**dic)

    return HttpResponse("Hello world")

方法二
在 views.py 文件上修改

from django.shortcuts import render, HttpResponse
from app01 import models
from  app01.models import Book,Author

def data_oper(req):
    dic = { "title":"K8S", "price":1, "publication_date":"2019-05-02", "publisher_id":1 }   # 直接指定 publisher 外键值了,这里要加 _id 
    Book.objects.create(**dic)

    return HttpResponse("Hello world")

外键多对多的情况

正向查询

from django.shortcuts import render, HttpResponse
from app01 import models
from  app01.models import Book,Author,Publisher

# Create your views here.

def data_oper(req):
    book=models.Book.objects.filter(id=1)[0]
    authors=models.Author.objects.filter(id__gt=2)
    book.authors.add(*authors)

    return HttpResponse("Hello world")

反向查询

from django.shortcuts import render, HttpResponse
from app01 import models
from  app01.models import Book,Author,Publisher

# Create your views here.

def data_oper(req):
    book = models.Book.objects.filter(id__gt=1)
    authors = models.Author.objects.filter(id=1)[0]
    authors.book_set.add(*book)

    return HttpResponse("Hello world")

双下划线(__)之单表条件查询

#    models.Book.objects.filter(id__lt=10,id__gt=1)   # 获取id大于1 且 小于10的值
#
#    models.Book.objects.filter(id__in=[11, 22, 33])   # 获取id等于11、22、33的数据
#    models.Book.objects.exclude(id__in=[11, 22, 33])  # not in
#
#    models.Book.objects.filter(title__contains="ven")
#    models.Book.objects.filter(title__icontains="ven") # icontains大小写不敏感
#
#    models.Book.objects.filter(id__range=[1, 2])   # 范围bettwen and

猜你喜欢

转载自www.cnblogs.com/klvchen/p/10881460.html
今日推荐