Reputation: 139
I have following models
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'
and
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'
I want to add unique_together
on SettingAttrValue
and User
. If i do this in meta class of second model django is giving me error like 'unique_together' refers to a ManyToManyField 'user', but ManyToManyFields are not permitted in 'unique_together'.
Upvotes: 1
Views: 2191
Reputation: 2675
Add unique_together
to the intermediate model:
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')
Upvotes: 2