I have a Django model that represents a financial model. This model has a one-to-many relationship with another set of models that also contain financial data. In my database I store the values in USD ($), however when I come to use the models I want to be able to convert between EURs and GBP.
Therefore, I have added a method in my models called convert_to_ccy(rate) and if I call this I want it to convert the related models as well (via this one-to-many relationship). Here is a very simple example to illustrate what I am trying to do.
class main_model_example(models.Model):
INCLUDE_IN_CCY_CONVERSION = ['value_field_1', 'value_field_2' etc...]
# FIELD DEFINITIONS
date = models.DateField()
value_field_1 = models.DecimalField(max_digits=20, decimal_places=2, default=0.00)
value_field_2 = models.DecimalField(max_digits=20, decimal_places=2, default=0.00)
...etc...
# METHODS
def convert_to_ccy(self, rate):
"""Converts $ fields"""
for field in self._meta.get_all_field_names():
if field in self.INCLUDE_IN_CCY_CONVERSION:
value = getattr(self, field)
if value != None:
setattr(self, field, float(value) / rate)
# NOW CONVERT THE RELATED MODELS
for position in self.the_related_model_set.all():
position.convert_to_ccy_rate(rate)
# THE RELATED MODEL
class the_related_model(models.Model):
INCLUDE_CCY_CONVERSION = ['yet_another_finacial_field']
main_model_example = models.ForeignKey(main_model_example, on_delete=models.CASCADE)
yet_another_financial_field = models.DecimalField(max_digits=20, decimal_places=2, default=0.00)
...and so on....
def convert_to_ccy(self, rate):
"""Converts $ fields"""
for field in self._meta.get_all_field_names():
if field in self.INCLUDE_IN_CCY_CONVERSION:
value = getattr(self, field)
if value != None:
setattr(self, field, float(value) / rate)
This works, and converts the correct fields. In my code (views.py etc) I pass around the main model which retains the new values I have set (ie in EURs).
However, when I come to get information off the related models again (via the main model using model.the_related_model_set.all() the values are back to being in USD. I assume this is because the xxx_set.all() method retrieves the models from the database again (which has NOT been updated). What I would like is once I have updated the related models, for them to remain in memory so when I come to use them again the retain the updated values I have set. I have read the documentation and browsed questions but cannot seem to find anything similar.
So I have two questions:
- Is what I want to achieve possible? I.e updating the related (one-to-many) models in memory, passing around the main model (doing more calculations) and then getting back the updated values when I reference the related models again later.
Or do I need to get all the related models off the main model at the off-set and assign them to a variable which i have to pass around with the main model?
- Is the fact that I am doing it this way considered bad? I.e. having the main models method call the related models methods? I am not overly comfortable with my implementation which often suggests there is a better way.
Any advice on better ways to achieve this would be greatly appreciated.
Thanks.