"Cannot delete some instances of model '%s' because they are " "referenced through a protected foreign key: '%s.%s'" % ( field.remote_field.model.__name__, sub_objs[0].__class__.__name__, field.name ), sub_objs ) def SET(value): """ SET(), 此时需要指定 set 的值, 括号里...
from django.dbimportmodelsclassCategory(models.Model):name=models.TextField()parent_cat=models.ForeignKey('self',on_delete=models.CASCADE) on_delete参数如下: CASCADE:级联操作。如果外键对应的那条数据被删除了,那么这条数据也会被删除。 PROTECT:受保护。即只要这条数据引用了外键的那条数据,那么就不能删...
Django 的ForeignKey 是一种逻辑上的两个表的关联关系,可以指定是否使用数据库的 FOREIGN KEY 约束。 如: from django.db import models class Province(models.Model): name = models.CharField(max_length=16) def __unicode__(self): return self.name class City(models.Model): name = models.CharField(m...
删除时会引起 ProtectedError,不删除关联表的内容:param collector::param field::param sub_objs::param using::return:""" raiseProtectedError("Cannot delete some instances of model '%s' because they are ""referenced through a protected foreign key: '%s.%s'"%(field.remote_field.model.__name__,su...
class Place(models.Model): name = models.CharField(max_length=50) address = models.CharField(max_length=80) def __unicode__(self): return u"%s the place" % self.name class Restaurant(models.Model): place = models.OneToOneField(Place, primary_key=True) ...
Model): person = models.ForeignKey(Person, on_delete=models.CASCADE) group = models.ForeignKey(Group, on_delete=models.CASCADE) date_joined = models.DateField() invite_reason = models.CharField(max_length=64) When you set up the intermediary model, you explicitly specify foreign keys to the...
If you change the value of the primary key on an existing object and then save it, a new object will be created alongside the old one. For example: from django.db import models class Fruit(models.Model): name = models.CharField(max_length=100, primary_key=True) >>> fruit = Fruit....
Gender = models.ForeignKey('Coding',on_delete=models.CASCADE) # 关联Coding表格 ForeignKey (othermodel,on_delete,**options) 有两个必选的参数 : 第一个参数:关联的表格(主表),在默认的情况下,外键储存的是主表的主键(Primary key)。上面的例子里,关联到 Coding 表格,默认情况下储存 Coding 表格的主键 ...
living = models.ForeignKey(City, related_name="citizen", on_delete=models.CASCADE) def __unicode__(self): return self.firstname +self.lastname 对select_related 的操作,主要针对以上model来进行说明: 常用 model.tb.objects.all().select_related('外键字段') ...
class English(models.Model): tag = models.ManyToManyField(to=Tag, related_name="notes", null=True) source = models.ForeignKey(Source, on_delete=models.CASCADE, null=True) 我们向English表中加入一条数据,还要向数据库记录一对多的Source-English,和多对多的Tag-English的关系。如下: def submit(request...