Guest User

Django MultiValueField with RadioSelect example

a guest
Dec 5th, 2010
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1.  
  2. @H@CHOICES = (('x', 'Choice x'), ('y', 'Choice y'))
  3.    
  4. class MyWidget(MultiWidget):    
  5.     def __init__(self, attrs=None):
  6.         widgets = (RadioSelect(choices=CHOICES, attrs=attrs),
  7.                    TextInput(attrs=attrs))
  8.         super(MyWidget, self).__init__(widgets, attrs)
  9.        
  10.     def decompress(self, value):
  11.         if value:
  12.             return value
  13.         return ['x', 'hello',]
  14.  
  15.  
  16. class MyField(MultiValueField):        
  17.     def __init__(self, *args, **kwargs):
  18.         fields = (
  19.             ChoiceField(choices=CHOICES),
  20.             CharField(max_length=100))
  21.         super(MyField, self).__init__(fields=fields, *args, **kwargs)
  22.  
  23.     def compress(self, data_list):
  24.         if data_list:
  25.             return [data_list[0], data_list[1]]
  26.         return [None, None]
Advertisement
Add Comment
Please, Sign In to add comment