Reputation: 34972
I'm facing a strange issue with django. It seems that when I run is_valid() on the formset, one value of each subform is set to None. To check this, I put two debug lines printing the cleaned_data dict. One at the end of cleaned_qty(), and the other right after is_valid().
Here is the form :
class ProductsForm(forms.Form):
product_ref = forms.CharField(max_length=6, widget=forms.HiddenInput)
product_name = forms.CharField(max_length=200, widget=forms.HiddenInput)
unit = forms.CharField(max_length=16, widget=forms.HiddenInput)
qty = forms.DecimalField(max_digits=6, decimal_places=3,
widget=forms.TextInput(attrs={'size': 5}))
def clean_qty(self):
data = self.cleaned_data
if data['qty'] < 0:
raise ValidationError('La quantité doit être positive')
unit = Unit.objects.get(pk=data['unit'])
if not unit.accept_dec and '.' in str(data['qty']) \
and int(str(data['qty']).split('.')[1]) != 0:
raise ValidationError('La quantité doit être entière')
print "!!!" + str(data) + "!!!"
ProductsFormSet = formsets.formset_factory(ProductsForm, extra=0,
can_delete=True)
And here is the beginning of the view :
def cart(request):
if request.method == 'POST':
products_formset = ProductsFormSet(request.POST, prefix='products')
cart_form = CartForm(request.POST, prefix='cart')
if products_formset.is_valid() and cart_form.is_valid():
'''
Create and save the cart
Send a confirmation email
'''
for form in products_formset:
print "???" + str(form.cleaned_data) + "???"
And the result when I submit my form :
DEBUG:django.db.backends:(0.000) SELECT `catalog_unit`.`value`, `catalog_unit`.`plural_val`, `catalog_unit`.`accept_dec` FROM `catalog_unit` WHERE `catalog_unit`.`value` = 'Truc' ; args=(u'Truc',)
!!!{'product_ref': u'BDL233', 'product_name': u'Bidule', 'unit': u'Truc', 'qty': Decimal('2.2')}!!!
DEBUG:django.db.backends:(0.001) SELECT `catalog_unit`.`value`, `catalog_unit`.`plural_val`, `catalog_unit`.`accept_dec` FROM `catalog_unit` WHERE `catalog_unit`.`value` = 'Truc' ; args=(u'Truc',)
!!!{'product_ref': u'MCH024', 'product_name': u'Machin', 'unit': u'Truc', 'qty': Decimal('1.3')}!!!
DEBUG:django.db.backends:(0.000) SELECT `catalog_unit`.`value`, `catalog_unit`.`plural_val`, `catalog_unit`.`accept_dec` FROM `catalog_unit` WHERE `catalog_unit`.`value` = 'Chacal' ; args=(u'Chacal',)
!!!{'product_ref': u'CHO127', 'product_name': u'Chouette', 'unit': u'Chacal', 'qty': Decimal('3')}!!!
???{'DELETE': False, 'product_ref': u'BDL233', 'product_name': u'Bidule', 'unit': u'Truc', 'qty': None}???
???{'DELETE': False, 'product_ref': u'MCH024', 'product_name': u'Machin', 'unit': u'Truc', 'qty': None}???
???{'DELETE': False, 'product_ref': u'CHO127', 'product_name': u'Chouette', 'unit': u'Chacal', 'qty': None}???
As you can see, the only difference is that qty is set to None.
I don't have any idea from where this behavior can come from.
Thanks in advance for you light.
Upvotes: 0
Views: 250
Reputation: 599630
All clean_FIELD
methods must return the value being validated. So your clean_qty
method should have return data['qty']
.
Upvotes: 2