[Django] #20442: Stale ContentType objects cause a NoneType error in get_for_id

8 views
Skip to first unread message

Django

unread,
May 18, 2013, 9:37:16 AM5/18/13
to django-...@googlegroups.com
#20442: Stale ContentType objects cause a NoneType error in get_for_id
--------------------------------------+--------------------
Reporter: vdboor | Owner: vdboor
Type: Uncategorized | Status: new
Component: contrib.contenttypes | Version: 1.4
Severity: Normal | Keywords: dceu13
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
When content types are stale an error occurs when using
`ContentType.objects.get_for_id()`:

{{{
Traceback (most recent call last):
...
File
"/Users/diederik/Sites/webapps/django/django/contrib/contenttypes/tests.py",
line 280, in <lambda>
test = ContentType.objects.get_for_id(ct.pk)
File
"/Users/diederik/Sites/webapps/django/django/contrib/contenttypes/models.py",
line 107, in get_for_id
self._add_to_cache(self.db, ct)
File
"/Users/diederik/Sites/webapps/django/django/contrib/contenttypes/models.py",
line 122, in _add_to_cache
key = (model._meta.app_label, model._meta.model_name)
AttributeError: 'NoneType' object has no attribute '_meta'
}}}

The object can still be fetched, but `ct.model_class()` returns `None`
which causes the `_add_to_cache` function to operate on a NoneType.

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

Django

unread,
May 18, 2013, 9:41:24 AM5/18/13
to django-...@googlegroups.com
#20442: Stale ContentType objects cause a NoneType error in get_for_id
-------------------------------------+-------------------------------------
Reporter: vdboor | Owner: vdboor
Type: Bug | Status: assigned
Component: | Version: 1.4
contrib.contenttypes | Resolution:
Severity: Normal | Triage Stage:
Keywords: dceu13 | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => assigned
* needs_better_patch: => 0
* type: Uncategorized => Bug
* needs_tests: => 0
* needs_docs: => 0


Comment:

I was thinking about solving this in a few possible ways:

* Let `_add_to_cache` use `(ct.app_label, ct.model)` as key instead of the
model object. I think they are always identical, so this would allow
caching the stale content type. Nothing changes for calling code, they
still need to check whether `ct.model_class()` returns `None` as it
explicitly does already.

* Raise a `StaleContentTypeError` in `get_for_id()`. This would require
updating code everywhere else too.

* Raise a `ContentType.DoesNotExist`. This gives consistent output from
`ContentType.objects.get_for_id()` I don't like it as will leak out the
inconsistency elsewhere.

My preference goes to the first option.

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

Django

unread,
May 18, 2013, 11:44:48 AM5/18/13
to django-...@googlegroups.com
#20442: Stale ContentType objects cause a NoneType error in get_for_id
-------------------------------------+-------------------------------------
Reporter: vdboor | Owner: vdboor

Type: Bug | Status: assigned
Component: | Version: 1.4
contrib.contenttypes | Resolution:
Severity: Normal | Triage Stage:
Keywords: dceu13 | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* has_patch: 0 => 1


Comment:

When evaluating the solutions with my local projects, it turns out that
option nr1 (return the content type) is the best solution.
The `get_for_id()` returns the `ContentType` object as promised, and like
any other operation (e.g. `ContentType.objects.all()`), the caller would
have to check for `ct.model_class() is None`.

Pull request: https://github.com/django/django/pull/1130

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

Django

unread,
May 19, 2013, 8:33:26 AM5/19/13
to django-...@googlegroups.com
#20442: Stale ContentType objects cause a NoneType error in get_for_id
-------------------------------------+-------------------------------------
Reporter: vdboor | Owner: vdboor
Type: Bug | Status: closed
Component: | Version: 1.4
contrib.contenttypes | Resolution: fixed

Severity: Normal | Triage Stage:
Keywords: dceu13 | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Andrew Godwin <andrew@…>):

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


Comment:

In [changeset:"9b22baddef1a6676f82fe7c112894cc8de045334"]:
{{{
#!CommitTicketReference repository=""
revision="9b22baddef1a6676f82fe7c112894cc8de045334"
Merge pull request #1130 from vdboor/issue_20442_get_for_id

Fixed #20442: NoneType error when fetching a stale ContentType with
get_for_id
}}}

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

Django

unread,
Mar 28, 2014, 3:14:26 AM3/28/14
to django-...@googlegroups.com
#20442: Stale ContentType objects cause a NoneType error in get_for_id
-------------------------------------+-------------------------------------
Reporter: vdboor | Owner: vdboor
Type: Bug | Status: closed
Component: | Version: 1.6

contrib.contenttypes | Resolution: fixed
Severity: Normal | Triage Stage:
Keywords: dceu13 | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* version: 1.4 => 1.6


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

Reply all
Reply to author
Forward
0 new messages