我在模型中使用unique_together约束。
class Facility(models.Model):
institute = models.ForeignKey(Institute)
name = models.CharField(max_length=100)
class Meta:
unique_together = ('institute', 'name',)
如何获取具有相同名称和机构的重复值?
答案 0 :(得分:0)
设置meta unique_together
当你试图在DB Django中保存重复值时,它意味着不允许这样做并抛出异常 ,表示DB中没有重复值。
根据您的模型结构,如果您不想在数据库中保存重复条目,请先删除所有先前的条目,然后运行make migrations and migrate。