-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamically set currency #11
Comments
I'd rather create a |
Django still doesn't support the multiple db columns field: |
@derenio: same issue, so I've tried your example. I get Price instance without currency (Price('10', currency=None)). |
@spout my 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) |
@derenio: Thank you ! |
One of our projects requires the currency of the
PriceField
to be dynamically set.Our solution is to define
get_currency
method on thePriceField
which can be overridden by class inheritance.master...derenio:feature/dynamic_currency
Exemplary use case:
The text was updated successfully, but these errors were encountered: