Hey,

It would help a lot to see your code posted to dpaste.com or somewhere. As
far as grabbing the choices in your clean method, here's a little
copy-and-paste from Django's own ChoiceField so you can see how they do it:

class ChoiceField(Field):
>
>     widget = Select
>
>     default_error_messages = {
>
>         'invalid_choice': _(u'Select a valid choice. %(value)s is not one
>> of the available choices.'),
>
>     }
>
>
>>     def __init__(self, choices=(), required=True, widget=None, label=None,
>
>                  initial=None, help_text=None, *args, **kwargs):
>
>         super(ChoiceField, self).__init__(required=required,
>> widget=widget, label=label,
>
>                                         initial=initial,
>> help_text=help_text, *args, **kwargs)
>
>         self.choices = choices
>
>
>>     def __deepcopy__(self, memo):
>
>         result = super(ChoiceField, self).__deepcopy__(memo)
>
>         result._choices = copy.deepcopy(self._choices, memo)
>
>         return result
>
>
>>     def _get_choices(self):
>
>         return self._choices
>
>
>>     def _set_choices(self, value):
>
>         # Setting choices also sets the choices on the widget.
>
>         # choices can be any iterable, but we call list() on it because
>
>         # it will be consumed more than once.
>
>         self._choices = self.widget.choices = list(value)
>
>
>>     choices = property(_get_choices, _set_choices)
>
>
>>     def to_python(self, value):
>
>         "Returns a Unicode object."
>
>         if value in validators.EMPTY_VALUES:
>
>             return u''
>
>         return smart_unicode(value)
>
>
>>     def validate(self, value):
>
>         """
>
>         Validates that the input is in self.choices.
>
>         """
>
>         super(ChoiceField, self).validate(value)
>
>         if value and not self.valid_value(value):
>
>             raise ValidationError(self.error_messages['invalid_choice'] %
>> {'value': value})
>
>
>>     def valid_value(self, value):
>
>         "Check to see if the provided value is a valid choice"
>
>         for k, v in self.choices:
>
>             if isinstance(v, (list, tuple)):
>
>                 # This is an optgroup, so look inside the group for options
>
>                 for k2, v2 in v:
>
>                     if value == smart_unicode(k2):
>
>                         return True
>
>             else:
>
>                 if value == smart_unicode(k):
>
>                     return True
>
>         return False
>
>
So, if you're creating your own ChoiceField, it looks like you'll need to
do quite a bit of work. If you really need your own ChoiceField, it might
be better to just subclass theirs.. However, I don't think you're doing
anything too custom where Django's ChoiceField isn't enough. If you can
post your code somewhere, I'll keep an eye out for this thread and try to
help as much as I can.



On Wed, May 9, 2012 at 3:39 AM, HDayi <hacid...@gmail.com> wrote:

> hi Kurtis and Frances
> after populating the ChoiceField it looks like this
>    <select id="id_teacher" name="teacher">
>        <option value="First">First</option>
>        <option value="Second">Second</option>
>    </select>
> at first values were number but django form returns the value for this
> ChoiceField after validation.
> I don't have a clean_teacher method because i don't know what to do.
>
> i think the problem is that: when the form is first created coice list for
> this ChoiceField is empty. after that what ever it returns is not included
> in the choice list because it's empty. i need to add a choice list in the
> clean method but i don't know how.
>
> when i gogled everybody was talking about a web page (django gotchas or
> someting like that) but link was always broken. :(
>
>  --
> 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/-/FHF67cmYklQJ.
>
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to