2

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 serialized text shows up in the admin text field?

Jordan Reiter
  • 20,467
  • 11
  • 95
  • 161
  • are you saying that admin does not show the content of a model's text field? please clarify the question. – akonsu Sep 07 '11 at 05:05
  • The field is a custom field with a `to_python` method. So when it retrieves the value from the database, it deserializes it. I want the serialized version to show up as the content of the text field. – Jordan Reiter Sep 07 '11 at 15:01

2 Answers2

0

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.

Community
  • 1
  • 1
Oiva Eskola
  • 949
  • 8
  • 13
0

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.

Udi
  • 29,222
  • 9
  • 96
  • 129
  • So which method in the `Field` class do I add so that the value is re-serialized? I thought that would be value_to_string but it isn't. Looks like `formfield_overrides` allows you to change general, basic options for all fields of a given type. This wouldn't let me change the value of the field. – Jordan Reiter Sep 07 '11 at 15:02