Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get form field value before passing the form validation?

Tags:

django

My question is related to this link: How to allow user to input comma

So I'm trying to do all the possibility just to make it work. Now I'm trying to get the value pass when performing saving action before it validates by the form.is_valid().

I'm successfully get the value by doing this:

.........    
if request.method == 'POST':
    if request.POST['process'] == 'addtrans':
        tform = AddTransactionForm(request.user, 
                                   request.POST)

        print tform.fields['amount'] // this is the value that I want to get

        if tform.is_valid():
..........

But sadly the output is this:

<django.forms.fields.DecimalField object at 0x7fcc84fd2c50>

How to get the exact value or decode that output? I hope someone have tried this one.

like image 936
catherine Avatar asked Jan 22 '26 15:01

catherine


1 Answers

I think this is what you're describing -

def transaction(request): 
    if request.POST.method == 'POST':
        post = request.POST.copy()
        if 'amount' in post:
            post['amount'] = post['amount'].replace(',','')
        tform = AddTransactionForm(request.user, post)
        #...

(you have to create a copy of the request.POST dictionary because it's immutable).

like image 102
Aidan Ewen Avatar answered Jan 27 '26 01:01

Aidan Ewen