#7551: GenericForeignKey fails if initialized to None during model init
-------------------------------------+-------------------------------------
     Reporter:  SamBull              |                    Owner:
         Type:  Bug                  |                   Status:  new
    Component:                       |                  Version:  master
  contrib.contenttypes               |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:  contenttypes         |      Needs documentation:  0
  genericforeignkey                  |  Patch needs improvement:  1
    Has patch:  1                    |                    UI/UX:  0
  Needs tests:  0                    |
Easy pickings:  0                    |
-------------------------------------+-------------------------------------
Description changed by ramiro:

Old description:

> Supposing you have a model with a GenericForeignKey...
>
> {{{
> #!python
> 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:
> {{{
> #!python
> ex = Example(name='foobar')
> }}}
> But if you do this...
> {{{
> #!python
> 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.

New description:

 Supposing you have a model with a GenericForeignKey...

 {{{
 #!python
 class Example(models.Model):
     name = models.CharField()
     content_type = models.ForeignKey(ContentType, null=True, blank=True)
     object_id = models.PositiveIntegerField(null=True, blank=True)
     obj = GenericForeignKey()
 }}}

 If you initialize like this, everything is fine:
 {{{
 #!python
 ex = Example(name='foobar')
 }}}
 But if you do this...
 {{{
 #!python
 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.

--

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to