#10427: Bound field needs an easy way to get form value
-------------------------------+--------------------------------------------
          Reporter:  toxik     |         Owner:  nobody          
            Status:  reopened  |     Milestone:                  
         Component:  Forms     |       Version:  SVN             
        Resolution:            |      Keywords:  form field value
             Stage:  Accepted  |     Has_patch:  1               
        Needs_docs:  0         |   Needs_tests:  0               
Needs_better_patch:  0         |  
-------------------------------+--------------------------------------------
Comment (by jameshfis...@gmail.com):

 To update the filters by zanuxzan:

 {{{
 from django.forms import ChoiceField

 from django import template
 register = template.Library()

 @register.filter(name='field_value')
 def field_value(field):
         """
         Returns the value for this BoundField, as rendered in widgets.
         """
         if field.form.is_bound:
                 if isinstance(field.field, FileField) and field.data is
 None:
                         val = field.form.initial.get(field.name,
 field.field.initial)
                 else:
                         val = field.data
         else:
                 val = field.form.initial.get(field.name,
 field.field.initial)
                 if callable(val):
                         val = val()
         if val is None:
                 val = ''
         return val

 @register.filter(name='display_value')
 def display_value(field):
         """
         Returns the displayed value for this BoundField, as rendered in
 widgets.
         """
         value = field_value(field)
         if isinstance(field.field, ChoiceField):
                 for (val, desc) in field.field.choices:
                         if val == value:
                         return desc
         return value
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/10427#comment:23>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--

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


Reply via email to