Opened 13 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)
Change History (10)
by , 13 years ago
Attachment: | db.models.fields.related-fix-subclassed.patch added |
---|
comment:1 by , 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.
comment:2 by , 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:4 by , 12 years ago
Triage Stage: | Unreviewed → 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 by , 11 years ago
Cc: | added |
---|
comment:6 by , 11 years ago
Cc: | removed |
---|
comment:7 by , 11 years ago
Needs tests: | set |
---|
comment:8 by , 9 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
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.
Patch to make subclassed fields work as forein key