多表查询

多表查询

表结构

from django.db import models

# Create your models here.

class Author(models.Model):
    """
    作者表
    """
    name=models.CharField( max_length=32)
    age=models.IntegerField()
    # authorDetail=models.OneToOneField(to="AuthorDetail",to_field="id",on_delete=models.CASCADE)  #
    au=models.OneToOneField("AuthorDetail",on_delete=models.CASCADE)

class AuthorDetail(models.Model):
    """
    作者详细信息表
    """
    birthday=models.DateField()
    telephone=models.CharField(max_length=11)
    addr=models.CharField(max_length=64)
    # class Meta:
        # db_table='authordetail' #指定表名
        # ordering = ['-id',]
class Publish(models.Model):
    """
    出版社表
    """
    name=models.CharField( max_length=32)
    city=models.CharField( max_length=32)

class Book(models.Model):
    """
    书籍表
    """
    title = models.CharField( max_length=32)
    publishDate=models.DateField()
    price=models.DecimalField(max_digits=5,decimal_places=2)
    publishs=models.ForeignKey(to="Publish",on_delete=models.CASCADE,)
    authors=models.ManyToManyField('Author',)

​ 注: 自动创建第五张表

增删改查

增加

# 一对一
# 第一种方法
# 类属性作为关键字时,值为model对象
au_obj = models.AuthorDetail.objects.get(id=4)
models.Author.objects.create(
     name='海狗',
     age=59,
     au=au_obj
    )

# 第二种方法
# 关键字为数据库字段名称, 那么值为关联数据的值
models.Author.objects.create(
     name='海狗',
     age=59,
     au_id=4
    )

# 一对多
pub_obj = models.Publish.objects.get(id=3)
    models.Book.objects.create(
        title='xx2',
        price=13,
        publishDate='2011-11-12',
        publishs=pub_obj 
    )

# 多对多
ziwen = models.Author.objects.get(id=3)
haigou = models.Author.objects.get(id=5)
new_obj = models.Book.objects.create(
        title='海狗产后护理第二部',
        price=0.5,
        publishDate='2019-09-29',
        publishs_id=2,
    )

new_obj.authors.add(3,5)  # *args  **kwargs
new_obj.authors.add(*[3,5])  # 用的最多
new_obj.authors.add(ziwen, haigou)

删除

# 一对一
models.AuthorDetail.objects.filter(id=3).delete()
models.Author.objects.filter(id=3).delete()

# 一对多
models.Publish.objects.filter(id=3).delete()
models.Book.objects.filter(id=4).delete()

ret = models.Publish.objects.get(id=2)
models.Book.objects.filter(id=5).update(
    # title='华丽丽',
    publishs=ret,
)

多对多的增删改查

book_obj = models.Book.objects.get(id=2)
book_obj.authors.add()  # 添加
book_obj.authors.remove(1)  #删除
book_obj.authors.clear()  # 清除
book_obj.authors.set(['1','5'])  # 先清除再添加,相当于修改

基于对象的跨表查询

​ 关系属性写在表1, 关联到表2, 通过表1的数据去找表2的数据, 叫做正向查询, 反过来就是反向查询

# 一对一
# 正向查询  对象.属性
obj = models.Author.objects.filter(name='王洋').first()
ph = obj.au.telephone
print(ph)

# 反向查询 对象.小写的表名
obj = models.AuthorDetail.objects.filter(telephone=120).first()
ret = obj.author.name  # 陈硕
print(ret)


# 一对多
# 正向查询 对象.属性
obj = models.Book.objects.filter(title='海狗的怂逼人生').first()
ret = obj.publishs.name
print(ret) 

# 反向查询 对象.小写表名_set.all()
obj = models.Publish.objects.filter(name='24期出版社').first()
ret = obj.book_set.all() 
for i in ret:
    print(i.title)
    
    
# 多对多
# 正向查询 对象.属性.all()
obj = models.Book.objects.filter(title='海狗的怂逼人生').first()
ret = obj.authors.all()
print(ret)  
for i in ret:
    print(i.name)

# 反向查询 对象.小写表名_set.all()
obj = models.Author.objects.filter(name='海狗').first()
ret = obj.book_set.all()
print(ret)
for i in ret:
    print(i.publishs.name)
    print(i.title)

admin操作

添加用户

python manage.py createsuperuser
输入用户名: wuchao
邮箱不用输入, 直接回车
输入密码:必须超过8位, 并且别太简单

admin注册

from django.contrib import admin
from app01 import models

admin.site.register(models.Author)
admin.site.register(models.AuthorDetail)
admin.site.register(models.Publish)
admin.site.register(models.Book)
posted @ 2019-11-26 17:48  边城bei  阅读(81)  评论(0编辑  收藏  举报