bryansis2010 bryansis2010 - 7 months ago 11
Python Question

Ticket amount resolves to Null in Django Form

I have a simple TicketForm which is a Django Form. I want to override the cleanfield methods in Django 1.8

class TicketForm(forms.Form):
ticket_amount = forms.DecimalField(label="Ticket Amount", required=True)
name = forms.CharField(label="Name",required=False)

def clean_ticket_amount(self):
ticket_amount = self.cleaned_data.get('ticket_amount', None)
if ticket_amount <= 0.00:
raise forms.ValidationError("Ticket values cannot be below $0.00")

def save(self):
print(self.cleaned_data)
#some other code


When I do something like
print(self.cleaned_data)
I get an output as so:

{'name': "some_name", 'ticket_amount': {...}}


And then I cannot process it. Otherwise I've seen the
'ticket_amount'= NoneType
error. Did I implement the clean methods wrongly?

Answer

The answer is to put return ticket_amount at the end of the method.

Comments