DefaultValueWidget

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class DefaultValueWidget(forms.widgets.Widget):
    def __init__(self, value, display=None, attrs=None):        
        if isinstance(display, forms.ModelChoiceField):
            try:
                object = display.queryset.get(pk=value)
                self.display = str(object)
            except:
                self.display = None
        # this allows to genericly pass in any field object intending to to
        # catch ModelChoiceFields, without having to care about the actual
        # type.
        elif isinstance(display, forms.Field):
            self.display = None
        else:
            self.display = display
        self.value = value
        super(DefaultValueWidget, self).__init__(attrs)    
        
    def value_from_datadict(self, data, name):
        return self.value
    
    def render(self, name, value, attrs=None):
        if self.display is None: return self.value
        else: return self.display

More like this

  1. Honeypot Field by SmileyChris 6 years, 2 months ago
  2. Automatically trim newforms text fields by miracle2k 5 years, 8 months ago
  3. "Approved" field with timestamp by miracle2k 5 years, 10 months ago
  4. Widget for CommaSeparatedIntegerField with pre-defined choices by rudyryk 2 years, 11 months ago
  5. ManyToManyField no syncdb by powerfox 4 years, 4 months ago

Comments

(Forgotten your password?)