Skip to content

Instantly share code, notes, and snippets.

@martinsam
Created January 6, 2011 11:14
Show Gist options
  • Save martinsam/767776 to your computer and use it in GitHub Desktop.
Save martinsam/767776 to your computer and use it in GitHub Desktop.
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("...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment