django forms - overriding constructor for changing field type depending by input

132 views
Skip to first unread message

luke lukes

unread,
May 19, 2012, 8:06:23 PM5/19/12
to django...@googlegroups.com
hi everyone. i need to overrid the constructor of a form: if i have some value in input, some fields has to be ChoiceField, otherwise they have to be CharField. here's the code:


but this way doesn't work. even if i declare fields before constructor  as CharField and then I ovverride them in __init__, they will not be renderized in template (I got this message: <django.forms.fields.ChoiceField object at 0x7ff0c0173ad0>).I don't understand where's the mistake (also because i have poor python skills...)

any help?

Mario Gudelj

unread,
May 19, 2012, 9:25:36 PM5/19/12
to django...@googlegroups.com
Hey Luke,

I don't think you need to return anything, especially not a super class.


Cheers,

-m



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/lIzEfD5QeZMJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

luke lukes

unread,
May 20, 2012, 7:49:28 AM5/20/12
to django...@googlegroups.com
Hi. already tried this way --> fields are instantiated (<django.forms.fields.ChoiceField object at 0x7f6204206f10>) but are not rendered in the template...
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

luke lukes

unread,
May 20, 2012, 8:11:50 AM5/20/12
to django...@googlegroups.com

Daniel Roseman

unread,
May 20, 2012, 8:28:21 AM5/20/12
to django...@googlegroups.com
On Sunday, 20 May 2012 12:49:28 UTC+1, luke lukes wrote:
Hi. already tried this way --> fields are instantiated (<django.forms.fields.ChoiceField object at 0x7f6204206f10>) but are not rendered in the template...

You should be assigning your new fields to `self.fields['fieldname']`, not `self.fieldname`.
--
DR. 

Paul Prior

unread,
May 20, 2012, 8:21:39 AM5/20/12
to django...@googlegroups.com
I think you need self.fields[field_name] = your new field.
Paul


To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/a67HgD8GqvgJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

luke lukes

unread,
May 20, 2012, 11:28:11 AM5/20/12
to django...@googlegroups.com
thanks Paul and DR, your solution worked. anyway now i have a problem with the validation of that form (her's the view):

def anagrafica_save(request):
    if request.method == 'POST':
        form = AnagraficaForm(request.POST)
        if'idsogg' in request.POST:
            soggetto = Anagrafica.objects.get(pk=request.POST["idsogg"])
        else:
            soggetto = Anagrafica()
        
        if form.is_valid():
   .... #set model fields and save
   ...
else:
   print('errors',form.errors)
            sub = "Scheda soggetto anagrafica"
            dict = {'sub':sub, 'subsogg':soggetto, 'form':form}
            return render_to_response('anagrafica_form.html', dict, context_instance=RequestContext(request))

The form is always not valid, also the form.errors print show an empty dict. The form is returned back and instead of renderized ChoiceFields as select, i got these messages printed in html: <django.forms.fields.ChoiceField object at 0x7ff264215110>...any help?

bruno desthuilliers

unread,
May 21, 2012, 7:36:20 AM5/21/12
to Django users


On May 20, 5:28 pm, luke lukes <lordluk...@gmail.com> wrote:
> thanks Paul and DR, your solution worked. anyway now i have a problem with
> the validation of that form (her's the view):


(snip)

Sorry, your code's indentation get messed up so it's hard to tell
where the problem ise exactly. Please post a link to dpaste or
something similar...

Oh and yes, just in case : wrt/ the form, Paul and Daniel are right
about the assignment to self.fields[fieldname], but you also need to
call the parent's __init__ before.

Reply all
Reply to author
Forward
0 new messages