I have a model in Django as follows
class TwoPlanetKeyword(models.Model):
planet_one = models.ForeignKey(Planet, related_name="planet_one")
planet_two = models.ForeignKey(Planet, related_name="planet_two")
keyword_list = models.TextField(max_length=100000)
class Meta:
verbose_name = 'Keywords for Two Planet Combination'
unique_together = ['planet_one', 'planet_two']
def __str__(self):
return "Keywords for two planet combination of {} and {}".format(self.planet_one, self.planet_two)
def clean(self, *args, **kwargs):
plan_one = self.planet_one
plan_two = self.planet_two
try:
obj_val = TwoPlanetKeyword.objects.get(Q(planet_one=plan_one, planet_two=plan_two) | Q(planet_one=plan_two, planet_two=plan_one))
raise ValidationError({
NON_FIELD_ERRORS: [
'This combination exists',
],
})
except TwoPlanetKeyword.DoesNotExist:
super(TwoPlanetKeyword, self).clean(*args, **kwargs)
def full_clean(self, *args, **kwargs):
return self.clean(*args, **kwargs)
def save(self, *args, **kwargs):
self.full_clean()
super(MyModel, self).save(*args, **kwargs)
The idea here is that to basically prevent permutations of fields in the table from being entered as detailed here Prevent permutations of foreign keys in Django.
This solves the above problem but it gives me a clean() got an unexpected keyword argument 'validate_unique'
error when I try to save new entries in the table in Django Admin