jeudi 26 mai 2016

ValueObject pattern in django models

Currently I want to use the Value Object Pattern in my django project. I have a class Price like this:

class Price(object):
    def __init__(self, value, currency):
       self.value = value
       self.currency = currency

And now I want to use this class in a normal model. The question is simple, how should I use it in the model? What type of field?

My most important requirement is, that I can access the price directly on an instance of the model. So that I could write something like this:

item.price.in(Currency.EURO)

So I don't need to call some methods to invoke for example deserialization, if I store the price as JSON in the database.

Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire