迁移
- 分两步实现
- 迁移文件的生成
- 根据models文件生成对应的迁移文件
- 根据models和已有迁移文件差别 生成新的迁移文件
- 执行迁移文件
- 先去迁移记录查找,哪些文件未迁移过
- 执行未迁移的文件
- 执行完毕,记录执行过的迁移文件
- 重新迁移
一对一 1:1
- 应用场景
- Django中 OneToOneField
- 实现
- 数据删除
- 级联表
- 主表
- 从表
- 谁声明关系谁就是从表
- 在开发中如何确认主从
- 当系统遭遇不可避免毁灭时,只能保留一张表,这个表就是你的主表
- 默认特性(CASECADE)
- 从表数据删除,主表不受影响
- 主表数据删除,从表数据直接删除
- PROTECT 受保护
- 开发中为了防止误操作,我们通常会设置为此模式
- 主表如果存在级联数据,删除动作受保护,不能成功
- 主表不存在级联数据,可以删除成功
- SET
- SET_NULL
- SET_DEFAULT
- SET()
- 级联数据获取
- 主获取从 隐性属性 默认就是级联模型的名字
- 从获取主,显性属性,就是属性的名字
一对多 1:N
- ForeignKey
- 主从获取
- 主获取从 隐性属性 级联模型_set
- student_set Manager的子类
- all
- filter
- exclude
- Manager上能使用的函数都能使用
- 从获取主
多对多M:N
-
- 实际上最复杂
- 开发中很少直接使用多对多属性,而是自己维护多对多的关系
- 产生表的时候会产生单独的关系表
- 关系表中存储关联表的主键,通过多个外键实现的
- 多个外键值不能同时相等
- 级联数据获取
- 级联数据
- ManyRelatedManager
代码
modle.py
from django.db import models
class Person(models.Model):
p_name = models.CharField(max_length=16)
p_sex = models.BooleanField(default=False)
class IDCard(models.Model):
id_num = models.CharField(max_length=18, unique=True)
id_person = models.OneToOneField(Person, null=True, blank=True, on_delete=models.SET_NULL)
class Customer(models.Model):
c_name = models.CharField(max_length=16)
class Goods(models.Model):
g_name = models.CharField(max_length=16)
g_customer = models.ManyToManyField(Customer)
class Animal(models.Model):
a_name = models.CharField(max_length=16)
class Meta:
abstract = True
class Cat(Animal):
c_eat = models.CharField(max_length=32)
class Dog(Animal):
d_legs = models.IntegerField(default=4)
urls.py
urlpatterns = [
url(r'^hello/', views.hello, name='hello'),
url(r'^addperson/', views.add_person, name='add_person'),
url(r'^addidcard/', views.add_idcard, name='add_id_card'),
url(r'^bindcard/', views.bind_card, name='bind_card'),
url(r'^removeperson/', views.remove_person, name='remove_person'),
url(r'^removeidcard/', views.remove_idcard, name='remove_idcard'),
url(r'^getperson/', views.get_person, name='get_person'),
url(r'^getidcard/', views.get_idcard, name='get_idcard'),
url(r'^addcustomer/', views.add_customer, name='add_customer'),
url(r'^addgoods/', views.add_goods, name='add_goods'),
url(r'^addtocart/', views.add_to_cart, name='add_to_cart'),
url(r'^getgoodslist/(?P<customerid>\d+)/', views.get_goods_list, name='get_goods_list'),
url(r'^addcat/', views.add_cat, name='add_cat'),
url(r'^adddog/', views.add_dog, name='add_dog'),
]
views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
from Two.models import Person, IDCard, Customer, Goods, Cat, Dog
def hello(request):
return HttpResponse("Two Hello")
def add_person(request):
username = request.GET.get("username")
person = Person()
person.p_name = username
person.save()
return HttpResponse("Person创建成功 %d" % person.id)
def add_idcard(request):
id_num = request.GET.get("idnum")
idcard = IDCard()
idcard.id_num = id_num
idcard.save()
return HttpResponse("IDCard %d" % idcard.id)
def bind_card(request):
person = Person.objects.last()
idcard = IDCard.objects.last()
idcard.id_person = person
idcard.save()
return HttpResponse("绑定成功")
def remove_person(request):
person = Person.objects.last()
person.delete()
return HttpResponse("人员移除成功")
def remove_idcard(request):
idcard = IDCard.objects.last()
idcard.delete()
return HttpResponse("身份证移除成功")
def get_person(request):
idcard = IDCard.objects.last()
person = idcard.id_person
return HttpResponse(person.p_name)
def get_idcard(request):
person = Person.objects.last()
idcard = person.idcard
return HttpResponse(idcard.id_num)
def add_customer(request):
c_name = request.GET.get("cname")
customer = Customer()
customer.c_name = c_name
customer.save()
return HttpResponse("创建消费者成功%d" % customer.id)
def add_goods(request):
g_name = request.GET.get("gname")
goods = Goods()
goods.g_name = g_name
goods.save()
return HttpResponse("创建商品成功%d" % goods.id)
def add_to_cart(request):
customer = Customer.objects.last()
goods = Goods.objects.last()
'''
print(type(goods.g_customer))
print(goods.g_customer)
goods.g_customer.add(customer)
'''
customer.goods_set.add(goods)
return HttpResponse("添加成功")
def get_goods_list(request, customerid):
customer = Customer.objects.get(pk=customerid)
goods_list = customer.goods_set.all()
return render(request, 'goods_list.html', context=locals())
def add_cat(request):
cat = Cat()
cat.a_name = "Tom"
cat.c_eat = "Fish"
cat.save()
return HttpResponse("Cat 创建成功 %d" % cat.id)
def add_dog(request):
dog = Dog()
dog.a_name = "Tom"
dog.save()
return HttpResponse("Dog 创建成功 %d" % dog.id)
templatesgood_list.html
<ul>
{% for goods in goods_list %}
<li>{{ goods.g_name }}{{ goods.id }}</li>
{% endfor %}
</ul>
继承