Created
January 6, 2011 11:14
-
-
Save martinsam/767776 to your computer and use it in GitHub Desktop.
Django unique_together work with ForeignKey=None
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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