Code

Opened 6 years ago

Closed 5 years ago

#5826 closed (fixed)

Can't recover GenericForeignKey content type and object id field names

Reported by: kgoudeaux Owned by: nobody
Component: Contrib apps Version: master
Severity: Keywords: ContentType GenericForeignKey generic relations
Cc: koen.biermans@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The GenericForeignKey class is perfect for recovering the object it references but leaves no way to recover itself and the field names it stores.

Its get function only returns the object it references or an error.
DoesNotExist when it isn't set on an instance.
AttributeError when accessed from the model class.

This one line patch and doc test changes the behavior of get to return the GenericForeignKey instance when accessed from the model class thereby allowing access to the content type and primary key field names.

Attachments (1)

generic.diff (1.2 KB) - added by kgoudeaux <kgoudeaux@…> 6 years ago.
generic.py patch and generic relations doc test

Download all attachments as: .zip

Change History (6)

Changed 6 years ago by kgoudeaux <kgoudeaux@…>

generic.py patch and generic relations doc test

comment:1 Changed 6 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

You need to go through Model._meta.fields to get at this info.

comment:2 Changed 6 years ago by kgoudeaux@…

  • Resolution wontfix deleted
  • Status changed from closed to reopened

The information for each field is in Meta, of course, but there is no way to determine which fields correspond with what for a generic foreign key.
You can guess 'content_type' and 'object_id' but this won't always be the case.

It seems reasonable that GenericForeignKey would allow some way to programmatically determine which fields in Meta that it references when interacting with the Class, as opposed to an instance.

comment:3 Changed 6 years ago by Koen Biermans <koen.biermans@…>

  • Cc koen.biermans@… added

I just tried this to make auditTrail (as described in the wiki) also provide the genericforeign key on the original item. Works perfectly.
Definitely a good change.

comment:4 Changed 6 years ago by jacob

  • Triage Stage changed from Unreviewed to Design decision needed

comment:5 Changed 5 years ago by Koen Biermans <koen.biermans@…>

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

It seems this was fixed in r9562.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.