Opened 16 years ago

Last modified 11 years ago

#7551 closed

GenericForeignKey fails if initialized to None during model init — at Initial Version

Reported by: Sam Bull Owned by: nobody
Component: contrib.contenttypes Version: dev
Severity: Normal Keywords: contenttypes genericforeignkey
Cc: sbull@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Supposing you have a model with a GenericForeignKey...

class Example(models.Model):
    name = models.CharField()
    ct = models.ForeignKey(ContentType, null=True, blank=True)
    obj_id = models.PositiveIntegerField(null=True, blank=True)
    obj = GenericForeignKey()

If you initialize like this, everything is fine:

ex = Example(name='foobar')

But if you do this...

ex = Example(name='foobar', obj=None)

You get an exciting error:
Impossible arguments to GFK.get_content_type!

It looks like the GFK tries to initialize the content_type and object_id fields if its attribute is one of the ones passed to the init. In fact, it should follow the example of get and only initialize those fields if the argument passed to it is non-None.

Change History (1)

by Sam Bull, 16 years ago

GFK-init patch

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