#14059: ForeignKey Field validates using the default database rather than the
current model instance's database
------------------------------------------------+---------------------------
 Reporter:  [email protected]  |       Owner:  nobody    
   Status:  new                                 |   Milestone:            
Component:  Forms                               |     Version:  1.2       
 Keywords:  multipledatabases                   |       Stage:  Unreviewed
Has_patch:  1                                   |  
------------------------------------------------+---------------------------
 When using multiple databases, ForeignKey fields can be setup to pull
 their options from a specific database.
 ForeignKey.formfield({ 'using' : 'NotDefault' }).
 However when validating the form, the ForeignKey fields do not validate
 using the associated database.

 Current form validation method incorrectly validates ForeignKey
 relationships using the default manager instead of the model instances'
 database.

 Django 1.2 Code: django.db.models.fields.related.py
 {{{
 class ForeignKey(RelatedField, Field):

     def validate(self, value, model_instance):
         if self.rel.parent_link:
             return
         super(ForeignKey, self).validate(value, model_instance)
         if value is None:
             return

         qs =
 self.rel.to._default_manager.filter(**{self.rel.field_name:value})
         qs = qs.complex_filter(self.rel.limit_choices_to)
         if not qs.exists():
             raise
 exceptions.ValidationError(self.error_messages['invalid'] % {
                 'model': self.rel.to._meta.verbose_name, 'pk': value})
 }}}

 Proposed change will using the model_instances' database instance of the
 default manager.
 As you can see '''self.rel.to._default_manager''' becomes
 '''self.rel.to._default_manager.using(model_instance._state.db)'''.
 {{{

     def validate(self, value, model_instance):
         if self.rel.parent_link:
             return
         super(ForeignKey, self).validate(value, model_instance)
         if value is None:
             return

         qs =
 
self.rel.to._default_manager.using(model_instance._state.db).filter(**{self.rel.field_name:value})
         qs = qs.complex_filter(self.rel.limit_choices_to)
         if not qs.exists():
             raise
 exceptions.ValidationError(self.error_messages['invalid'] % {
                 'model': self.rel.to._meta.verbose_name, 'pk': value})
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/14059>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
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