Re: Difficulty validating ModelMultipleChoiceField

2007-07-06 Thread danylo

Problem solved thanks to Adrian.

Added:
from django.utils.datastructures import MultiValueDict

and I added the following to the SortedCheckBox widget:

def value_from_datadict(self, data, name):
if isinstance(data, MultiValueDict):
return data.getlist(name)
return data.get(name, None)



On Jul 5, 9:30 pm, danylo <[EMAIL PROTECTED]> wrote:
> I can't get a form to validate and for the life of me, can't figure
> out why.
>
> I keep getting an error on the topics.  Regardless of how many I
> choose, it returns an "Enter a list of values." error.  It looks like
> even though the form returns a list of topics, it doesn't send the
> list to the validator, just a single value.
>
> I've also tried converting the topics bit to a MutlipleChoiceField,
> but didnt' have any mroe luck with it.
>
> Any suggestions of where to look are much appreciated.  Thanks,
>
> dan
>
> class AddBlogForm(forms.Form):
> def __init__( self, *args, **kwargs ):
> super( AddBlogForm, self ).__init__( *args, **kwargs )
> self.fields['location'] =
> GroupedChoiceField(choices=location_drop_tags())
> self.fields['topics'] = forms.ModelMultipleChoiceField(
> queryset=Tags.objects.exclude(location__exact=True).order_by('-
> parent_tag', 'tag'), widget=SortedCheckBox)
> owner = forms.CharField()
> password = forms.CharField(widget=forms.PasswordInput)
> url = forms.URLField(initial='http://', verify_exists=True)
> title = forms.CharField(max_length=100)
> description = forms.CharField(max_length=500, required=False,
> widget=forms.Textarea(attrs={'rows': '3'}))
>
> (this is the widget used for the topic)
> class SortedCheckBox(forms.Select):
> def render(self, name, value, attrs=None, choices=()):
> from django.utils.html import escape
> from django.newforms.util import flatatt, smart_unicode
> final_attrs = self.build_attrs(attrs, name=name)
> output = []
> if value is None:
> value = ''
> str_value = tuple([smart_unicode(v) for v in value])
> for t in
> self.choices.queryset.filter(parent_tag__isnull=True):
> output.append(u'')
> option_value = smart_unicode(t.id)
> option_label = smart_unicode(t.tag)
> selected_html = (option_value in str_value) and u'checked
> ' or ''
> output.append(u' value="%s" %s/>%s' % (option_value, selected_html,
> option_label))
> for t_child in
> self.choices.queryset.filter(parent_tag__tag=t.tag):
> option_value = smart_unicode(t_child.id)
> option_label = smart_unicode(t_child.tag)
> selected_html = (option_value in str_value) and
> u'checked ' or ''
> output.append(u' value="%s" %s/>%s' % (option_value, selected_html,
> option_label))
> output.append(u'')
> return u'\n'.join(output)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Difficulty validating ModelMultipleChoiceField

2007-07-05 Thread danylo


I can't get a form to validate and for the life of me, can't figure
out why.

I keep getting an error on the topics.  Regardless of how many I
choose, it returns an "Enter a list of values." error.  It looks like
even though the form returns a list of topics, it doesn't send the
list to the validator, just a single value.

I've also tried converting the topics bit to a MutlipleChoiceField,
but didnt' have any mroe luck with it.

Any suggestions of where to look are much appreciated.  Thanks,

dan


class AddBlogForm(forms.Form):
def __init__( self, *args, **kwargs ):
super( AddBlogForm, self ).__init__( *args, **kwargs )
self.fields['location'] =
GroupedChoiceField(choices=location_drop_tags())
self.fields['topics'] = forms.ModelMultipleChoiceField(
queryset=Tags.objects.exclude(location__exact=True).order_by('-
parent_tag', 'tag'), widget=SortedCheckBox)
owner = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
url = forms.URLField(initial='http://', verify_exists=True)
title = forms.CharField(max_length=100)
description = forms.CharField(max_length=500, required=False,
widget=forms.Textarea(attrs={'rows': '3'}))



(this is the widget used for the topic)
class SortedCheckBox(forms.Select):
def render(self, name, value, attrs=None, choices=()):
from django.utils.html import escape
from django.newforms.util import flatatt, smart_unicode
final_attrs = self.build_attrs(attrs, name=name)
output = []
if value is None:
value = ''
str_value = tuple([smart_unicode(v) for v in value])
for t in
self.choices.queryset.filter(parent_tag__isnull=True):
output.append(u'')
option_value = smart_unicode(t.id)
option_label = smart_unicode(t.tag)
selected_html = (option_value in str_value) and u'checked
' or ''
output.append(u'%s' % (option_value, selected_html,
option_label))
for t_child in
self.choices.queryset.filter(parent_tag__tag=t.tag):
option_value = smart_unicode(t_child.id)
option_label = smart_unicode(t_child.tag)
selected_html = (option_value in str_value) and
u'checked ' or ''
output.append(u'%s' % (option_value, selected_html,
option_label))
output.append(u'')
return u'\n'.join(output)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---