1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

[soc2009/model-validation] Added validation for ForeignKey

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10877 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král
2009-06-01 15:41:56 +00:00
parent 32916adca8
commit 3ca1199da8

View File

@@ -682,6 +682,12 @@ class ForeignKey(RelatedField, Field):
if self.rel.parent_link:
return
super(ForeignKey, self).validate(value, model_instance)
if not value:
return
try:
self.rel.to._default_manager.get(**{self.rel.field_name:value})
except self.rel.to.DoesNotExist, e:
raise exceptions.ValidationError('Model %s with pk %r does not exist.' % (self.rel.to._meta.verbose_name, value))
def get_attname(self):
return '%s_id' % self.name