Opened 16 years ago

Closed 16 years ago

Last modified 13 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: Nicola Larosa Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 by Nicola Larosa, 16 years ago

Cc: Nicola Larosa added

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

comment:2 by Jacob, 16 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 by Jacob, 16 years ago

Resolution: fixed
Status: newclosed

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

comment:4 by Jacob, 16 years ago

(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 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

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