我有一个模特:
class Tag(models.Model):
name = models.CharField(max_length=30)
parent = models.ManyToManyField("self", null=True, blank=True)
def __str__(self):
return '{}'.format(self.name)
def clean(self):
if self.name:
if self.name == 'Minor Locality' and self.parent != 'City':
raise ValidationError({
NON_FIELD_ERRORS: ["Parent tag can't be other City tag "]
})
if self.name == 'Locality' and self.parent != 'City':
raise ValidationError({
NON_FIELD_ERRORS: ["Parent tag can't be other City tag "]
})
def save(self, *args, **kwargs):
self.clean()
super(Tag, self).save(*args, **kwargs)
当我尝试添加标记“City”并将其父级保留为空白时,会在保存时抛出错误:
ProgrammingError at /admin/locality/tag/add/
relation "locality_tag_parent" does not exist
LINE 1: DELETE FROM "locality_tag_parent" WHERE ("locality_tag_paren...