Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#9546 closed (fixed)

GenericForeignKey + model inheritance: uses wrong content type

Reported by: miracle2k Owned by: nobody
Component: Contrib apps Version: 1.0
Severity: Keywords:
Cc: tekNico Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

class Link(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_id")

class Base(models.Model):
    links = GenericRelation(Link)
class Sub(Base): pass
>>> link = Link()
>>> link.content_type = sub_instance
>>> link.save()   # saves with "sub" content type 

>>> sub_instance.links.all()   # queries with "base" content type 
[]
>>> sub_instance.base_ptr.links.all()  # queries with "base" content type 
[]

The expected result would contain the Link object just created, presumably in both cases, but at least in the first. Either GenericForeignKey should use the parent class content type, or GenericRelation uses the content type of the model it is accessed through.

There may be additional complexities involved when using multiple model inheritance, but I can't quite wrap my head around those so far.

Change History (5)

comment:1 Changed 6 years ago by tekNico

  • Cc tekNico added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I've been bitten by this one too...

comment:2 Changed 6 years ago by jacob

  • milestone set to 1.1
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

(In [10373]) Fixed #9546: GenericRelations inherited from base models no longer query using the wrong content type.

comment:4 Changed 6 years ago by jacob

(In [10374]) [1.0.X] Fixed #9546: GenericRelations inherited from base models no longer query using the wrong content type. Backport of r10373 from trunk.

comment:5 Changed 3 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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