views:

115

answers:

0

I'm trying to write a field for django admin that will have 2 widgets: a textinput and an associated checkbox. If the checkbox is checked it will return none, if not return the value of the field. But I can't get the notch of it. Here is how I tried(note I'm new to django):

class FloatRangeField(models.FloatField):

__metaclass__=models.SubfieldBase


def __init__(self,label,min_value=None,max_value=None,  **kwargs):
    self.label, self.min_value, self.max_value = label,min_value, max_value

    models.FloatField.__init__(self,  **kwargs)
def formfield(self, **kwargs):
    defaults = {'min_value': self.min_value, 'max_value':self.max_value}
    defaults.update(kwargs)
    return super(FloatRangeField, self).formfield(**defaults)
def clean(self, *value):
    print value[0]
    try:
        valoare_analiza, valoare_bifa=value
        if not valoare_bifa:
            return self.valoare_analiza
        else: return None
    except ValidationError, ve:
        raise ve
def compress(self, data_list):
    print('am ajuns la compress')
    print(data_list)
def to_python(self, value ):
    if not value:
        return ''
    if self.min_value is not None and self.max_value is None:
    if value<self.min_value:
        raise ValidationError('valoare introdusa tre sa fie mai mare de %f' %self.min_value)
    if self.max_value is not None and self.min_value is None:
        if value>self.max_value :
            raise ValidationError('valoarea introduse tre sa fie mai mica de %f' %self.max_value)
        if self.max_value is not None and self.min_value is not None:
    if value>self.max_value or value<self.min_value:
        raise ValidationError('valoarea introdusa tre sa fie intre %f si %f' %(self.min_value, self.max_value))
    return value

class CheckedWidget(forms.MultiWidget):

def __init__(self, target, attrs=None):

    widgets=(target,forms.CheckboxInput)
    super(CheckedWidget, self).__init__(widgets)


def format_output(self, rendered_widgets):
    return rendered_widgets[0] +'   '+rendered_widgets[1]+'bifeaza'
def decompress(self, value):
    if value is None:
        return [None, True]
    return [value, False]

class WidgetFinal(forms.MultiValueField):

def __init__(self, *args, **kwargs):
    fields=(FloatRangeField(*args, **kwargs) , forms.BooleanField('bifa'))
    widgets=CheckedWidget(forms.TextInput)
    super(WidgetFinal, self).__init__(fields, widget=widgets)

But this displays nothing. Any ideas?