Ticket #14691: 14691.diff

File 14691.diff, 1.6 KB (added by Ramiro Morales, 14 years ago)

Patch with tests

  • django/db/models/fields/related.py

    diff -r 41150ab49bb8 django/db/models/fields/related.py
    a b  
    835835        if value is None:
    836836            return
    837837
    838         qs = self.rel.to._default_manager.filter(**{self.rel.field_name:value})
     838        using = router.db_for_read(model_instance.__class__, instance=model_instance)
     839        qs = self.rel.to._default_manager.using(using).filter(
     840                **{self.rel.field_name: value}
     841             )
    839842        qs = qs.complex_filter(self.rel.limit_choices_to)
    840843        if not qs.exists():
    841844            raise exceptions.ValidationError(self.error_messages['invalid'] % {
  • tests/regressiontests/multiple_database/tests.py

    diff -r 41150ab49bb8 tests/regressiontests/multiple_database/tests.py
    a b  
    581581        self.assertEquals(Person.objects.using('other').count(), 0)
    582582        self.assertEquals(Pet.objects.using('other').count(), 0)
    583583
     584    def test_foreign_key_validation(self):
     585        "ForeignKey.validate() uses the correct database"
     586        mickey = Person.objects.using('other').create(name="Mickey")
     587        pluto = Pet.objects.using('other').create(name="Pluto", owner=mickey)
     588        self.assertEquals(None, pluto.full_clean())
     589
    584590    def test_o2o_separation(self):
    585591        "OneToOne fields are constrained to a single database"
    586592        # Create a user and profile on the default database
Back to Top