Opened 12 years ago

Closed 9 years ago

#18501 closed Bug (needsinfo)

Custom fields as foreign keys fix

Reported by: Melvyn Sopacua Owned by: nobody
Component: Database layer (models, ORM) Version: dev
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

Description

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

Download all attachments as: .zip

Change History (10)

by Melvyn Sopacua, 12 years ago

Patch to make subclassed fields work as forein key

comment:1 by Aymeric Augustin, 12 years ago

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.

by Melvyn Sopacua, 12 years ago

Attachment: bug18501.shar added

reproduction data

comment:2 by Melvyn Sopacua, 12 years ago

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

This looks similar to #17214 and #17122.

comment:4 by Aymeric Augustin, 12 years ago

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

Cc: maa@… added

comment:6 by maa@…, 11 years ago

Cc: maa@… removed

comment:7 by Tim Graham, 10 years ago

Needs tests: set

comment:8 by Tim Graham, 9 years ago

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