Opened 9 years ago

Closed 8 years ago

#26784 closed Bug (fixed)

foreign key validate() violates router API when no instance specified

Reported by: Ben Demboski Owned by: Ben Demboski
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

to reproduce:

  1. configure two databases: default and other
  2. have two models Person and Pet:
    class Person(models.Model):
        name = models.CharField(max_length=100)
    
    class Pet(models.Model):
        name = models.CharField(max_length=100)
        owner = models.ForeignKey(Person)
    
  3. have a database router enabled that routes models in the app containing person and pet (let's say its called petstore) to the other database:
    class PetStoreRouter(object):
        def db_for_read(self, model, **hints):
            if model._meta.app_label == 'petstore':
                return 'other'
            return None
    
        def db_for_write(self, model, **hints):
            if model._meta.app_label == 'petstore':
                return 'other'
            return None
    
  4. create a Person instance named person
  5. call clean() on the Pet model's owner field passing in persons pk, but no model:
    Pet._meta.get_field('owner').clean(person.pk, None)
    

The result will be a exception thrown from inside the router's db_for_read() method:

AttributeError: type object 'NoneType' has no attribute '_meta'

because the model argument contains <type 'NoneType'> (which doesn't have a _meta attribute) instead of a model class.

Change History (5)

comment:1 by Ben Demboski, 9 years ago

Status: newassigned

comment:3 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Tim Graham, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 2224a566:

Fixed #26784 -- Made ForeignKey.validate() pass model to router if model_instance=None.

Note: See TracTickets for help on using tickets.
Back to Top