How to access other model instance fields from to_python in a custom Django field implementation

557 views Asked by At

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

1

There are 1 answers

3
ozan On BEST ANSWER

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?