AttributeError raised when calling form's superclass clean() method, however the form doesn't get filled with errors

39 views
Skip to first unread message

Héctor Urbina

unread,
Dec 15, 2014, 1:51:04 PM12/15/14
to django...@googlegroups.com
Hello,

I have the following Form:

class TargetAllocationsForm(forms.Form):
 
def __init__(self, cliente, *args, **kwargs):
   
super(TargetAllocationsForm, self).__init__(*args, **kwargs)
   
for cat in CategoriaActivo.objects.all():
     
self.fields[cat] = forms.FloatField(label=cat.nombre, min_value=0, max_value=1)
     
try:
        currentAlloc
= cliente.targetCategoriaAllocations.filter(categoria=cat).latest("fecha")
       
self.fields[cat].initial = currentAlloc.allocation
     
except:
       
self.fields[cat].initial = cat.default_allocation
   
for sca in SubcategoriaActivo.objects.all():
     
self.fields[sca] = forms.FloatField(label=sca.nombre, min_value=0, max_value=1)
     
try:
        currentAlloc
= cliente.targetSubcategoriaAllocations.filter(subcategoria=sca).latest("fecha")
       
self.fields[sca].initial = currentAlloc.allocation
     
except:
       
self.fields[sca].initial = sca.default_allocation

 
def clean(self):
    cleaned_data
= super(TargetAllocationsForm, self).clean() # Here is where I think the error is being raised
    allocations
= {}
   
for k in self.fields.keys():
     
if isinstance(k, CategoriaActivo):
       
if k not in allocations:
          allocations
[k] = {"subcategorias":{}}
        allocations
[k]["alloc"] = cleaned_data.get(k)
     
elif isinstance(k, SubcategoriaActivo):
       
if k.categoria not in allocations:
          allocations
[k] = {"subcategorias":{}}
        allocations
[k.categoria]["subcategorias"][k] = cleaned_data.get(k)

    total_allocations_cat
= 0
   
for cat in allocations:
      total_allocations_cat
+= allocations[cat]["alloc"]
      total_alloc_subcat
= 0
     
for subcat in allocations[cat]["subcategorias"]:
        total_alloc_subcat
+= allocations[cat]["subcategorias"][subcat]
     
if total_alloc_subcat > 1:
       
raise forms.ValidationError("Total de allocations en subcategorías de %s supera el 100%" % str(cat))
   
if total_allocations_cat > 1:
     
raise forms.ValidationError("Total de allocations en categorías supera el 100%")



When receiving the form in request.POST, I call the form.is_valid() method and it returns False, but no errors on the form. If I manually call form.clean() method (using pdb.set_trace() just after form.is_valid()) I get

*** AttributeError: 'TargetAllocationsForm' object has no attribute 'cleaned_data'.

I would very much appreciate any help here

Greetings,
Hector.

Daniel Roseman

unread,
Dec 15, 2014, 2:23:36 PM12/15/14
to django...@googlegroups.com
You should show your view.

You should not call clean directly: it is called automatically by is_valid.

Also note that the keys of self.fields are strings, so neither your if nor your elif will ever execute.
--
DR.

Héctor Urbina

unread,
Dec 15, 2014, 2:39:35 PM12/15/14
to django...@googlegroups.com
This is my view:

def perfilInversionCliente(request, cliente_pk):
  objetos
= {}
  cliente
= get_object_or_404(Cliente, pk=cliente_pk)
  targetAllocationsForm
= TargetAllocationsForm(cliente=cliente)

 
if request.method == "POST":
    targetAllocationsForm
= TargetAllocationsForm(request.POST)
   
if targetAllocationsForm.is_valid():
      data
= targetAllocationsForm.cleaned_data
     
for k in data:
        editar
= False
       
if isinstance(k, CategoriaActivo):
         
try:
            currentAlloc
= cliente.targetCategoriaAllocations.filter(categoria=k).latest("fecha")
           
if currentAlloc.allocation != data[k]:
              editar
= True
         
except DoesNotExist:
            editar
= True
         
if editar:
            newAlloc
= TargetCategoriaAllocation(cliente=cliente, categoria=k, allocation=data[k], fecha=datetime.datetime.now(), editor=request.user)
            newAlloc
.save()
       
if isinstance(k, SubcategoriaActivo):
         
try:
            currentAlloc
= cliente.targetSubcategoriaAllocations.filter(subcategoria=k).latest("fecha")
           
if currentAlloc.allocation != data[k]:
              editar
= True
         
except DoesNotExist:
            editar
= True
         
if editar:
            newAlloc
= TargetSubcategoriaAllocation(cliente=cliente, subcategoria=k, allocation=data[k], fecha=datetime.datetime.now(), editor=request.user)
            newAlloc
.save()
   
else:
      pdb
.set_trace()

  objetos
["targetAllocationsForm"] = targetAllocationsForm
  categorias
= {}
 
for cat in CategoriaActivo.objects.all():
    categorias
[cat] = cat.subcategorias.all()
  objetos
["categorias"] = categorias
  objetos
["cliente"] = cliente
 
template = loader.get_template('carteras/perfilInversionista.html')
  context
= RequestContext(request, objetos)
 
return HttpResponse(template.render(context))


The keys of the form's fields are not strings, they are python objects... At first I though that wouldn't work, but I tested the form in a shell and it seems to work just fine.

Collin Anderson

unread,
Dec 17, 2014, 8:50:19 AM12/17/14
to django...@googlegroups.com
Hi Hector,

I think your clean() method needs to return cleaned_data.

Collin
Reply all
Reply to author
Forward
0 new messages