Django

Code

Ticket #13085 (new)

Opened 5 months ago

Last modified 5 months ago

GenericForeignKey.get_content_type fails if `object` or `id` evaluate to False. Should use 'is not None' test.

Reported by: benreynwar Assigned to: benreynwar
Milestone: Component: Contrib apps
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

contenttypes.GenericForeignKey.get_content_type is used to find the content type of an object. If the object evaluates to False, then an error is raised. This is because 'if obj:' is used, rather than 'if obj is not None:'. The same problem exists for the 'id' argument, and I believe also in the __get__ method.

Attachments

generic.py.diff (1.2 kB) - added by benreynwar on 03/10/10 23:19:35.
13085.diff (3.4 kB) - added by benreynwar on 03/14/10 17:40:26.
Patch with tests included

Change History

03/10/10 23:19:35 changed by benreynwar

  • attachment generic.py.diff added.

03/11/10 06:38:46 changed by russellm

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests set to 1.
  • needs_docs changed.

Two quick points:

1) Please generate patches relative to the root of the source tree 2) Please provide a test case, not just a patch. Under what conditions will the object be false? Ideally, this should be in the form of a regression test integrated into Django's test suite, but if you can't work out how to do that, a code sample will do.

Accepting because I think I can spot the problem case (getting the content type for an unsaved object)

03/13/10 14:17:44 changed by benreynwar

  • owner changed from nobody to benreynwar.

03/14/10 17:40:26 changed by benreynwar

  • attachment 13085.diff added.

Patch with tests included

03/14/10 17:44:42 changed by benreynwar

  • needs_tests deleted.

I've included a new patch with a regression test included. The object will be false if it has nonzero is defined and returns False, or if len is defined and returns 0. Either of these should be possible without generic objects failing. The bug would also cause problems if a generic object was used with content_type_id=0.


Add/Change #13085 (GenericForeignKey.get_content_type fails if `object` or `id` evaluate to False. Should use 'is not None' test.)




Change Properties
Action