Opened 6 years ago

Closed 4 years ago

#14325 closed Bug (duplicate)

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

Reported by: natano Owned by: 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


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 natano 6 years ago.
fix_genericforeignkey_comparison_and_add_regressiontest.diff (2.6 KB) - added by natano 6 years ago.
fix + regressiontest

Download all attachments as: .zip

Change History (11)

Changed 6 years ago by natano

comment:1 follow-up: Changed 6 years ago by Alex

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Changed 6 years ago by natano

fix + regressiontest

comment:2 in reply to: ↑ 1 Changed 6 years ago by natano

Replying to Alex:
regression test attached

comment:3 Changed 6 years ago by natano

  • Needs tests unset
  • Owner changed from nobody to natano
  • Status changed from new to assigned

comment:4 Changed 6 years ago by natano

  • Needs tests set

sry, accidentaly removed the needs_tests flag

comment:5 Changed 5 years ago by gabrielhurley

  • Component changed from Contrib apps to contrib.contenttypes

comment:6 Changed 5 years ago by graham_king

  • Severity set to Normal
  • Type set to Bug

comment:7 Changed 5 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:8 Changed 5 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:9 Changed 4 years ago by ramiro

  • Resolution set to duplicate
  • Status changed from assigned to closed

Duplicate of #13085.

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