Hi I am working on defining my own CurrencyField for newforms. The idea is to store the value (amount) in the DB by the smallest unit as an IntegerField. That is:
1,450.75 => 145075
The clean method will happily clean the incoming strings into the right
amount formatted as an integer:
In [2]: f = CurrencyField()
In [3]: f.clean("1,450.75")
Out[3]: 145075
My problem is when I need to present a bound form to the user. I do a
check to see if the value to be cleaned is some sort of int. If so - I
convert it to my special value, CurrencyValue.
As I read the source, the widget will apply smart_unicode (from
django.newforms.util) to the value given, thus I implemented a
__unicode__ method in CurrencyValue (and had it extend StrAndUnicode
(from django.newforms.util)).
However it seems as the data attribute of the BoundField gives me a raw
python int:
In [30]: bf = BoundField(f, field, 'price')
In [31]: unicode(bf)
Out[31]: u'<input type="text" name="price" value="1495" id="id_price" />'
In [32]: bf.data
Out[32]: 1495
In [33]: type(bf.data)
Out[33]: <type 'int'>
I can see (by inserting some print-commands in the clean() method) that
the clean() method has been activated - and indeed returns at line 43.
Looking at my code - I can't see how the clean() method should be able
to return an int? It will return either a CurrencyValue or None.
In short - why doesn't Out[31] give me value="14.95"?
Am I solving this problem the wrong way?
I realize that this is a little complicated, so please let my know if I
can clearify anything ;)
The source code for CurrencyValue and CurrencyField is here:
http://dpaste.com/14508/
Thank you,
--
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk | Visit us at: http://www.gmta.info
signature.asc
Description: OpenPGP digital signature

