如何确保不重复两个属性的唯一组合
例如,在以下模型中
class modelBodyPart(models.Model):
area = models.CharField(max_length=128)
crush_name = models.CharField(max_length=128)在modelBodyPart的每个实例中,area和crush_name应该总是不同的
例如,一些允许的和不允许的结果是:
area = Area_A crush_name=Jenny //OK
area = Area_A crush_name=Jordan //OK
area = Area_B crush_name=Jenny //OK
area = Area_A crush_name=Jenny //Not allowed我将如何在模型中实现这一点?我会使用together吗?我不能完全理解上面的链接中的案例要求,这就是我在这里问的原因。
发布于 2017-03-17 04:49:14
是的,你是对的,你的代码应该是这样的-
models.py
class modelBodyPart(models.Model):
area = models.CharField(max_length=128)
crush_name = models.CharField(max_length=128)
class Meta:
unique_together = ['area','crush_name']https://stackoverflow.com/questions/42848915
复制相似问题