Opened 5 years ago

Closed 19 months ago

#18501 closed Bug (needsinfo)

Custom fields as foreign keys fix

Reported by: Melvyn Sopacua 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 Melvyn Sopacua 5 years ago.
Patch to make subclassed fields work as forein key
bug18501.shar (7.5 KB) - added by Melvyn Sopacua 5 years ago.
reproduction data

Download all attachments as: .zip

Change History (10)

Changed 5 years ago by Melvyn Sopacua

Patch to make subclassed fields work as forein key

comment:1 Changed 5 years ago by Aymeric Augustin

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 5 years ago by Melvyn Sopacua

Attachment: bug18501.shar added

reproduction data

comment:2 Changed 5 years ago by Melvyn Sopacua

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 5 years ago by Łukasz Rekucki

This looks similar to #17214 and #17122.

comment:4 Changed 4 years ago by Aymeric Augustin

Triage Stage: UnreviewedAccepted

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 3 years ago by maa@…

Cc: maa@… added

comment:6 Changed 3 years ago by maa@…

Cc: maa@… removed

comment:7 Changed 3 years ago by Tim Graham

Needs tests: set

comment:8 Changed 19 months ago by Tim Graham

Resolution: needsinfo
Status: newclosed

As the sample project uses the deprecated SubfieldBase class to construct the custom field, I am going to close this ticket as "needsinfo". I suspect this issue may have been fixed since this ticket was created as UUIDField has been introduced into core and we fixed several related bugs.

Feel free to reopen with more details if it's still an issue.

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