Author: Honza_Kral
Date: 2009-06-01 10:41:56 -0500 (Mon, 01 Jun 2009)
New Revision: 10877

Modified:
   django/branches/soc2009/model-validation/django/db/models/fields/related.py
Log:
[soc2009/model-validation] Added validation for ForeignKey

Modified: 
django/branches/soc2009/model-validation/django/db/models/fields/related.py
===================================================================
--- django/branches/soc2009/model-validation/django/db/models/fields/related.py 
2009-06-01 15:41:41 UTC (rev 10876)
+++ django/branches/soc2009/model-validation/django/db/models/fields/related.py 
2009-06-01 15:41:56 UTC (rev 10877)
@@ -682,6 +682,12 @@
         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


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to