标签:for foreign cad count cas word user 模型 nbsp
表关系的实现
1 from django.db import models 2 3 # Create your models here. 4 5 class Account(models.Model): 6 username = models.CharField(max_length=20) 7 password = models.CharField(max_length=50) 8 phonenum = models.CharField(max_length=20, default=‘‘) 9 c_time = models.DateTimeField(auto_now_add=True,) # 创建时间 10 11 12 class Detail(models.Model): 13 num = models.CharField(max_length=10, default=‘‘) 14 hobby = models.CharField(max_length=10, default=‘‘) 15 account = models.OneToOneField(‘Account‘, on_delete=models.CASCADE)
在对应表里面新加字段,使用models.OnToOneField方法,第一个参数接收对应表的类的字符串,第二个设置关联,当被关联的表被删除的时候,该表也会被删除
1 from django.db import models 2 3 # Create your models here. 4 5 class Account(models.Model): 6 username = models.CharField(max_length=20) 7 password = models.CharField(max_length=50) 8 phonenum = models.CharField(max_length=20, default=‘‘) 9 c_time = models.DateTimeField(auto_now_add=True,) # 创建时间 10 many = models.ForeignKey(‘Many‘, on_delete=models.SET_NULL, null=True) 11 12 13 class Detail(models.Model): 14 num = models.CharField(max_length=10, default=‘‘) 15 hobby = models.CharField(max_length=10, default=‘‘) 16 account = models.OneToOneField(‘Account‘, on_delete=models.CASCADE) 17 18 19 class Many(models.Model): 20 num = models.CharField(max_length=10, default=‘‘) 21 hobby = models.CharField(max_length=10, default=‘‘)
在对应的表里设置主键(model.ForeignKey)
标签:for foreign cad count cas word user 模型 nbsp
原文地址:https://www.cnblogs.com/ivy-blogs/p/10700229.html