Comments (5)
@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.
I'd rather create a PriceAndCurrencyField
that stores the currency in a database field.
from django-prices.
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.
@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.
@derenio: Thank you !
from django-prices.
Related Issues (20)
- Support for Django 5
- Taxes are not being saved HOT 1
- BabelDjango not compatible with Django 1.10+
- AmountField value not displaying in admin form Field HOT 2
- Write tests involving models with TaxedMoneyField and save()/delete() calls
- TaxedMoneyField should be Django Field type HOT 3
- "amount" template filter ignores LC_NUMERIC environment variable HOT 6
- Decimal and int can be saved to MoneyFields
- Problem rendering number value on a form HOT 2
- Support For DRF?? HOT 2
- Django 2.2 'TaxedMoneyField' object has no attribute 'unique'
- Introduce snapshottests
- Enhance data fields in `format_price`
- Currency validation enhancements in multiwidget
- Integration tests with views
- Passing a widget instance to MoneyField produce crash HOT 1
- Cannot deserialize MoneyField HOT 1
- Add a long description to setup.py
- Updated_fields on MoneyField not working
- Update to django 4.0?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-prices.