我有以下模特
class SettingAttributes(Core):
attribute_name = models.CharField(max_length=100)
description = models.CharField(max_length=100)
setting_type = models.CharField(max_length=10, choices=SETTING_TYPES)
class Meta:
app_label = 'core'
和
class SettingAttrValue(Core):
attribute_value = models.CharField(max_length=200)
attribute_type = models.CharField(max_length=200)
attribute = models.ForeignKey(SettingAttributes)
user = models.ManyToManyField(User)
avaliases = models.TextField()
attriblob = models.BinaryField(default=None, blank=True, null=True)
class Meta:
app_label = 'core'
我想在unique_together
和SettingAttrValue
上添加User
。如果我在第二个模型的元类中执行此操作,django会给出类似'unique_together' refers to a ManyToManyField 'user', but ManyToManyFields are not permitted in 'unique_together'.
答案 0 :(得分:0)
将unique_together
添加到intermediate模型:
class SettingAttrValue(Core):
attribute_value = models.CharField(max_length=200)
attribute_type = models.CharField(max_length=200)
attribute = models.ForeignKey(SettingAttributes)
user = models.ManyToManyField(User, through='UserSettingAttrValue')
avaliases = models.TextField()
attriblob = models.BinaryField(default=None, blank=True, null=True)
class Meta:
app_label = 'core'
class UserSettingAttrValue(models.Model):
setting_attr_value = models.ForeignKey(SettingAttrValue)
user = models.ForeignKey(User)
class Meta:
unique_together = ('setting_attr_value', 'user')