Django, custom fields, to_python, and displaying serialized text as-is in Django admin
I have a complex object that I'm storing serialized in a text field. For most purposes, I want the object pulled from the database to be that complex object. However, when I'm editing it in a form, I just want to see that serialized text in the field.
I tried using the value_to_string
function but it appears as if it isn't being called at all when editing the record from with admin.
What do I do so that the raw开发者_如何学Go serialized text shows up in the admin text field?
Since the admin already uses the value of your model field, one option would be re-serializing it for editing...
See also formfield_overrides:
This provides a quick-and-dirty way to override some of the Field options for use in the admin. formfield_overrides is a dictionary mapping a field class to a dict of arguments to pass to the field at construction time.
I'm not sure if it's the same problem, but I figured out a way to change the field value in the admin form before displaying it. I explained how to use a custom widget and an custom admin form in this answer.
Note that the custom widget only helps you to display the value in a different format. It won't parse the input value back to an object, although I believe that would be possible too.
精彩评论