Code Monkey home page Code Monkey logo

Comments (5)

derenio avatar derenio commented on September 14, 2024 1

@spout my exemplary use case was based on my feature branch master...derenio:feature/dynamic_currency.
If you want to be able to have the currency based on a custom field (retrieved in the get_currency method) you will also have to override the changed to_python and formfield methods in the DynamicPriceField class (change self.currency to self.get_currency()):

class DynamicPriceField(PriceField):

    def get_currency(self):
        if not self.currency:
            self.currency = self.model.currency
        return self.currency

    def to_python(self, value):
        currency = self.get_currency()
        if isinstance(value, Price):
            if value.currency != currency:
                raise ValueError('Invalid currency: %r (expected %r)' % (
                    value.currency, currency))
            return value
        value = super(PriceField, self).to_python(value)
        if value is None:
            return value
        return Price(value, currency=currency)

    def formfield(self, **kwargs):
        defaults = {'currency': self.get_currency(),
                    'form_class': forms.PriceField}
        defaults.update(kwargs)
        return super(PriceField, self).formfield(**defaults)

from django-prices.

patrys avatar patrys commented on September 14, 2024

I'd rather create a PriceAndCurrencyField that stores the currency in a database field.

from django-prices.

derenio avatar derenio commented on September 14, 2024

Django still doesn't support the multiple db columns field:
https://code.djangoproject.com/ticket/5929
We will have to let programmers manually define the currency field and provide its name as a constructor's parameter in the price field.

from django-prices.

spout avatar spout commented on September 14, 2024

@derenio: same issue, so I've tried your example. I get Price instance without currency (Price('10', currency=None)).
(Using django-prices with django-prices-openexchangerates)

from django-prices.

spout avatar spout commented on September 14, 2024

@derenio: Thank you !

from django-prices.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.