views:

83

answers:

1

How can I access the model instance from a custom Django model field's to_python call? When I overrode pre_save, for example, I had model_instance as one of the arguments. to_python only receives the value. How can I do something like:

def to_python(self, value):
    value = value_based_on_another_field_from_model()
    return value

Thank you

+2  A: 

to_python is the wrong place to do whatever you're trying to do. You should merely be converting data (as returned by a database) to a python object. The model instance doesn't even exist at the time that to_python is called on your particular custom field instance, so you're definitely not going to have access to it. What is it that you are trying to achieve?

ozan
The model field is a specialized DateTimeField that converts dates to UTC on save based on a user profile's timezone for the model. It works great and I wanted to convert *back* to the user's timezone on load... I need access to the model's created_by field.
Harel
turns out I needed the value_from_object function. Problem solved!
Harel
Thanks Ozan for removing me from a wrong direction. Sometimes that what happens when you do work at 3am. :o)
Harel