#30368: prefetch_related does not work for GenericForeignKey field when its 
primary
key is also a foreign key
-------------------------------------+-------------------------------------
               Reporter:  Vinny Do   |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  master
  layer (models, ORM)                |       Keywords:  prefetch_related
               Severity:  Normal     |  genericforeignkey
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 **Steps to reproduce**

 Create `Base` model with an `AutoField` primary key
 {{{#!python
 class Base(models.Model):
     title = models.TextField()
 }}}

 Create a model where the primary key is also a foreign key
 {{{#!python
 class Extended(models.Model):
     base = models.OneToOneField(Base, on_delete=models.CASCADE,
 primary_key=True)
 }}}

 Create model with `GenericForeignKey`
 {{{#!python
 class Comment(models.Model):
     content_type = models.ForeignKey(ContentType,
 on_delete=models.CASCADE)
     object_pk = models.TextField()
     content_object = GenericForeignKey(ct_field="content_type",
 fk_field="object_pk")
 }}}

 Prefetch the GenericForeignKey field `content_object` expecting it to have
 a value but get `None` instead.
 {{{#!python
 # Setup
 base = Base.objects.create(title="foo")
 extended = Extended.objects.create(base=base)
 Comment.objects.create(content_object=extended)

 # Exercise
 comment = Comment.objects.prefetch_related("content_object").get()
 print(comment.content_object)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30368>
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/047.d663c3c8455b2f93d1e3b61dcd6cdb5c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to