Opened 2 years ago

Last modified 4 weeks ago

#18501 new Bug

Custom fields as foreign keys fix

Reported by: msopacua Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: RelatedField
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no


Custom fields which have a different database representation as python representation are not handled correctly when rendered as Select fields. The underlying cause is that the value on the model that has the foreign key is the database representation and it is not converted to python representation when model_to_dict is called.

The ForeignKey field subclasses RelatedField and Field, but RelatedField does not override it's value_from_object method to convert the value to python.

Attachments (2)

db.models.fields.related-fix-subclassed.patch (1.3 KB) - added by msopacua 2 years ago.
Patch to make subclassed fields work as forein key
bug18501.shar (7.5 KB) - added by msopacua 2 years ago.
reproduction data

Download all attachments as: .zip

Change History (9)

Changed 2 years ago by msopacua

Patch to make subclassed fields work as forein key

comment:1 Changed 2 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Could you provide a test case ? Or at least a minimal code sample and instructions to reproduce the problem?

That would be much easier for us than trying to rebuild an example from your description.

Changed 2 years ago by msopacua

reproduction data

comment:2 Changed 2 years ago by msopacua

Ok, so I'm unable to write a proper test case, because the ModelForm class does not expose the bug but the admin change form does. So the admin does something special, that I'm unable to figure out that exposes this bug.

Therefore I've provided an app "devices", that is a copy of the models involved reduced to the size that they still expose the bug. The test case therein as said, works properly. However, if one adds 'devices' to the installed apps of a project, load the fixture and then browse to http://localhost/admin/devices/devicepciid/1/ you will see that the vendor is not selected. Applying the patch, reloading application and refreshing the browser will show that the vendor is selected. I suspect the formfield callback to be the culprit but can't really figure out how to make this into a proper test case.

comment:3 Changed 21 months ago by lrekucki

This looks similar to #17214 and #17122.

comment:4 Changed 20 months ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted

Indeed, I could easily reproduce the problem with the example provided.

A quick scan of the models definition doesn't reveal anything suspicious.

comment:5 Changed 8 months ago by maa@…

  • Cc maa@… added

comment:6 Changed 8 months ago by maa@…

  • Cc maa@… removed

comment:7 Changed 4 weeks ago by timo

  • Needs tests set

Add Comment

Modify Ticket

Change Properties
<Author field>
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'

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

Note: See TracTickets for help on using tickets.