标签:objects asc 示例 cts auto Nid 版本 不用 integer
xx = models.OneToOneField(to='表名',to_field='字段名',on_delete=models.CASCADE)
#on_delete:删除时的一些级联效果,to_field可以不写,默认是关联到另一张表的主键,on_delete在1.x版本的django中不用写,默认是级联删除的,2.x版本的django要写.
new_author_detail = models.AuthorDetail.objects.create(
birthday='1979',
telephone='138',
addr='black'
)
# 方式1
models.Author.objects.create(
name='王涛',
age=18,
#直接将对象赋值属性
authorDetail=new_author_detail
)
new_author_detail = models.AuthorDetail.objects.create(
birthday='1979',
telephone='138',
addr='black'
)
# 方式2
models.Author.objects.create(
name='王涛',
age=18,
authorDetail_id=new_author_detail.id
)
表一外键关联到表二,表一删除,不影响表2,表2删除会影响表1
models.AuthorDetail.objects.get(id=2).delete()
models.Author.objects.get(id=3).delete()
models.Author.objects.filter(id=5).update(
name='崔老师',
age=16,
authorDetail=models.AuthorDetail.objects.get(id=5),
authorDetail_id=4,
)
xx = models.ForeignKey(to='表名',to_field='字段名',on_delete=models.CASCADE)
外键关联的直接赋值为某个对象即可
obj = models.Publish.objects.get(id=1)
models.Book.objects.create(
title='李帅',
publishDate='2019-07-22',
price=3,
publishs=obj
)
obj = models.Publish.objects.get(id=1)
models.Book.objects.create(
title='李帅test',
publishDate='2019-07-22',
price=3.5,
publishs_id=1 #可以是整型,也可以是对象的属性(id)
#publishs_id=obj.id
)
models.Publish.objects.get(id=1).delete()
models.Book.objects.get(nid=1).delete()
models.Book.objects.filter(pk=4).update(
title='B哥的往事2',
publishs=models.Publish.objects.get(id=3),
publishs_id=3, #注意级联关系 不能乱改
)
xx = models.ManyToManyField(to='另外一个表名') #这是自动创建第三表
book_obj = models.Book.objects.get(nid=1)
book_obj.authors.add(*[1,2])
book_obj = models.Book.objects.get(nid=6)
book_obj.authors.remove(6)#将某个特定的对象从被关联对象集合中去除。
book_obj.authors.remove(*[5,6])
book_obj.authors.clear() #情况与nid 6相关联的所有
book_obj.authors.add(*[1,])#添加表记录
book_obj.authors.set('1')#先清空然后在设置
book_obj.authors.set(['5','6']) #删除然后更新
from django.db import models
# Create your models here.
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="nid",on_delete=models.CASCADE)
authorDetail=models.OneToOneField(to='AuthorDetail') #一对一到AuthorDetail表 生成为表字段之后,会自动变为authorDetail_id这样有一个名称
# 外键字段 -- 外键字段名_id
# foreign+unique
def __str__(self):
return self.name
#作者详细信息表
class AuthorDetail(models.Model):
birthday=models.DateField()
# telephone=models.BigIntegerField()
telephone=models.CharField(max_length=32)
addr=models.CharField( max_length=64)
def __str__(self):
return self.addr
#出版社表 和 书籍表 是 一对多的关系
class Publish(models.Model):
name=models.CharField( max_length=32)
city=models.CharField( max_length=32)
email=models.EmailField() #charfield -- asdfasdf
def __str__(self):
return self.name
#书籍表
class Book(models.Model):
nid = models.AutoField(primary_key=True)
title = models.CharField( max_length=32)
publishDate=models.DateField()
price=models.DecimalField(max_digits=5,decimal_places=2) #decimal(16,2)
publishs=models.ForeignKey(to="Publish")
authors=models.ManyToManyField(to='Author',)
def __str__(self):
return self.title
标签:objects asc 示例 cts auto Nid 版本 不用 integer
原文地址:https://www.cnblogs.com/antmoe/p/12233311.html