Opened 14 years ago

Closed 12 years ago

#14325 closed Bug (duplicate)

GenericForeignKey fails if assigned to an object which implements the __len__ method

Reported by: Martin Natano Owned by: Martin Natano
Component: contrib.contenttypes Version: 1.2
Severity: Normal Keywords: contenttypes, genericforeignkey
Cc: natano@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Supposing you have a model with a GenericForeignKey:

class Example(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    instance = generic.GenericForeignKey()

and a model which implements the __len__ method:

class Table(models.Model)
    data = models.TextField()

    def __len__(self):
        return 0

If you do

t = Table.object.create()
e = Example()
e.instance = t

you get an error:

Impossible arguments to GFK.get_content_type!

I think this is cause by checking for a logical True value instead of a "not None" comparison.

Attachments (2)

fix_genericforeignkey_comparison.diff (725 bytes ) - added by Martin Natano 14 years ago.
fix_genericforeignkey_comparison_and_add_regressiontest.diff (2.6 KB ) - added by Martin Natano 14 years ago.
fix + regressiontest

Download all attachments as: .zip

Change History (11)

by Martin Natano, 14 years ago

comment:1 by Alex Gaynor, 14 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

by Martin Natano, 14 years ago

fix + regressiontest

in reply to:  1 comment:2 by Martin Natano, 14 years ago

Replying to Alex:
regression test attached

comment:3 by Martin Natano, 14 years ago

Needs tests: unset
Owner: changed from nobody to Martin Natano
Status: newassigned

comment:4 by Martin Natano, 14 years ago

Needs tests: set

sry, accidentaly removed the needs_tests flag

comment:5 by Gabriel Hurley, 14 years ago

Component: Contrib appscontrib.contenttypes

comment:6 by Graham King, 14 years ago

Severity: Normal
Type: Bug

comment:7 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 by Ramiro Morales, 12 years ago

Resolution: duplicate
Status: assignedclosed

Duplicate of #13085.

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