Tuesday, April 26, 2016

What's the best way to auto convert a model field values before it is rendered?

I need to store and retrieve temperatures in a model.  I'd like to store all the values in celsius.  However some users prefer to view the value in fahrenheit.
What is the recommended way to setup a model like this?  

Perhaps manually convert the values in the view and on save?
Or is it possible to use python properties in a model for saves, updates and gets? 
Or is some other practice preferred?

class Temp(models.Model):
    user = models.ForeignKey(User, unique=False)
    temperature = models.FloatField(null=True, blank=True)




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ee6de0ba-f1b6-4faf-a7eb-9ac47d0f3c38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment