percentage or number data entry solution please

99 views
Skip to first unread message

Mike Dewhirst

unread,
May 10, 2015, 8:45:00 PM5/10/15
to django...@googlegroups.com
In the Admin I want the user to be able to enter a decimal number with a
percentage sign (eg 33.3%) or without (eg 33.3) and to collect it as a
decimal number.

At the moment DecimalField barfs if the user enters a human style
percentage sign.

My initial thought is to change it to a CharField and muck around making
an awful mess behind the scenes to get - and store - the 0 to 100
decimal value I really want.

On the other hand, there is a snippet ...

https://djangosnippets.org/snippets/1914/

... which looks promising but I'm not sure how to get that into the Admin

I have seen Django Extras has a PercentField but that seems to be a lot
of stuff to solve a small "simple" problem. Also, I haven't seen where
to slip it into the Admin

There is a ticket #17662 (now closed 'wontfix') but that focuses on
doing conversions which isn't what I'm after.

Here is another approach based on the snippet quoted above ...

http://stackoverflow.com/questions/6690692/problem-rendering-a-django-form-field-with-different-representation

... and which draws a suggestion to use a MultiValueField. I didn't try
too hard to understand the Django docs on MultiValueField because if
that is the answer I'm trying to fix the wrong problem.

I just want to ignore the percent sign if the user sticks it into a
DecimalField.

Thanks for any ideas

Mike

Derek

unread,
May 11, 2015, 9:42:26 AM5/11/15
to django...@googlegroups.com
I realise this is probably overkill for your needs; but django-suit allows for a ton of customisation options for the admin, one of which is the ability to supply "units" as part of the data entry forms - see:
http://django-suit.readthedocs.org/en/latest/widgets.html#enclosedinput

Your other option is to provide functionality in the clean method to convert "33.3%" (which is a string ) into 33.3 (numeric) and then store the numeric value into another field... but that is messy.  I would rather have my label say, for example:

Score(%): ____

which is linked to a numeric only field.  And then have the form raise an error (custom if need be) on that field if the user tries to enter 33.3% rather than 33.3  (sometimes users have to be willing to learn as well :)

Mike Dewhirst

unread,
May 11, 2015, 10:26:25 AM5/11/15
to django...@googlegroups.com
On 11/05/2015 11:42 PM, Derek wrote:
> I realise this is probably overkill for your needs; but django-suit
> allows for a ton of customisation options for the admin, one of which is
> the ability to supply "units" as part of the data entry forms - see:
> http://django-suit.readthedocs.org/en/latest/widgets.html#enclosedinput
>
> Your other option is to provide functionality in the clean method to
> convert "33.3%" (which is a string ) into 33.3 (numeric) and then store
> the numeric value into another field... but that is messy. I would
> rather have my label say, for example:
>
> Score(%): ____
>
> which is linked to a numeric only field. And then have the form raise
> an error (custom if need be) on that field if the user tries to enter
> 33.3% rather than 33.3 (sometimes users have to be willing to learn as
> well :)

Which is what I am doing ... and will keep doing until there is an
enhancement along the lines of a DecimalField arg/kwarg which lets the
form accept a % sign but throws it away.

I have added some help text which explains what to do.

Thanks Derek

Mike
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/cebb28c2-9351-4d13-af3f-82c38d88b489%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/cebb28c2-9351-4d13-af3f-82c38d88b489%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages