Django unique_together work with ForeignKey=None
class FooBar(models.Model):
foo = models.ForeignKey(Foo)
bar = models.ForeignKey(Bar, null=True, blank=True)
class Meta:
unique_together = ('foo', 'bar')
def clean(self):
from django.core.exceptions import ValidationError
if self.foo and FooBar.objects.filter(name=self.bar).exists():
raise ValidationError("...")