[Django] #26784: foreign key validate() violates router API when no instance specified

3 views
Skip to first unread message

Django

unread,
Jun 20, 2016, 5:47:50 PM6/20/16
to django-...@googlegroups.com
#26784: foreign key validate() violates router API when no instance specified
----------------------------------------------+-------------------------
Reporter: bendemboski | Owner: bendemboski
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+-------------------------
to reproduce:
1. configure two databases: `default` and `other`
1. 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)
}}}
1. 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
}}}
1. create a Person instance named `person`
1. call `clean()` on the `Pet` model's `owner` field passing in `person`s
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.

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

Django

unread,
Jun 20, 2016, 5:48:38 PM6/20/16
to django-...@googlegroups.com
#26784: foreign key validate() violates router API when no instance specified
-------------------------------------+-------------------------------------
Reporter: bendemboski | Owner:
| bendemboski
Type: Bug | Status: assigned
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by bendemboski):

* status: new => assigned
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26784#comment:1>

Django

unread,
Jun 20, 2016, 6:02:41 PM6/20/16
to django-...@googlegroups.com
#26784: foreign key validate() violates router API when no instance specified
-------------------------------------+-------------------------------------
Reporter: bendemboski | Owner:
| bendemboski

Type: Bug | Status: assigned
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by bendemboski):

Patch is:
[https://github.com/django/django/pull/6813]

--
Ticket URL: <https://code.djangoproject.com/ticket/26784#comment:2>

Django

unread,
Jun 20, 2016, 8:59:25 PM6/20/16
to django-...@googlegroups.com
#26784: foreign key validate() violates router API when no instance specified
-------------------------------------+-------------------------------------
Reporter: bendemboski | Owner:
| bendemboski

Type: Bug | Status: assigned
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/26784#comment:3>

Django

unread,
Jun 27, 2016, 12:54:13 PM6/27/16
to django-...@googlegroups.com
#26784: foreign key validate() violates router API when no instance specified
-------------------------------------+-------------------------------------
Reporter: bendemboski | Owner:
| bendemboski

Type: Bug | Status: assigned
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/26784#comment:4>

Django

unread,
Jun 27, 2016, 1:34:39 PM6/27/16
to django-...@googlegroups.com
#26784: foreign key validate() violates router API when no instance specified
-------------------------------------+-------------------------------------
Reporter: bendemboski | Owner:
| bendemboski
Type: Bug | Status: closed

Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"2224a56631eae3342993e4b7b90e80f59a167151" 2224a566]:
{{{
#!CommitTicketReference repository=""
revision="2224a56631eae3342993e4b7b90e80f59a167151"
Fixed #26784 -- Made ForeignKey.validate() pass `model` to router if
model_instance=None.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26784#comment:5>

Reply all
Reply to author
Forward
0 new messages