views:

163

answers:

2

Hi,

is there something like getters and setters for django model's fields?

For example, I have a text field in which i need to make a string replace before it get saved (in the admin panel, for both insert and update operations) and make another, different replace each time it is read. Those string replace are dynamic and need to be done at the moment of saving and reading.

As I'm using python 2.5, I cannot use python 2.6 getters / setters.

Any help?

+1  A: 

There is no such thing as "python 2.6 getters / setters". Use properties.

Ignacio Vazquez-Abrams
+1  A: 

You can add a pre_save signal handler to the Model you want to save which updates the values before they get saved to the database.

It's not quite the same as a setter function since the values will remain in their incorrect format until the value is saved. If that's an acceptable compromise for your situation then signals are the easiest way to achieve this without working around Django's ORM.

Edit: In your situation standard Python properties are probably the way to go with this. There's a long standing ticket to add proper getter/setter support to Django but it's not a simple issue to resolve.

You can add the property fields to the admin using the techniques in this blog post

Dan Head
this could work, but what about the getter?
pistacchio