django - Search Form for NullBooleanField -


i write search form django model contains column is_ok nullbooleanfield.

i want have 4 choices:

  • search instances column true.
  • search instances column false.
  • search instances column null.
  • ignore column in search

up use this:

is_ok = forms.nullbooleanfield(required=false) 

but renders 3 options (as drop-down list).

how distinguish between "is null" , "not set" here?

as @alasdair said, you'll have create custom field:

class unsettype:     def __str__(self):         return '__unset__'  unset = unsettype()  class unsetornullbooleanselect(nullbooleanselect):     def __init__(self, attrs=none):         choices = (             ('__unset__', ugettext_lazy('unset')),             ('1', ugettext_lazy('unknown')),             ('2', ugettext_lazy('yes')),             ('3', ugettext_lazy('no')),         )         super().__init__(attrs, choices)      def render(self, name, value, attrs=none):         try:             value = {                 true: '2', false: '3', none: '1',                 '2': '2', '3': '3', '1': '1',             }[value]         except keyerror:             value = '__unset__'         return super().render(name, value, attrs)      def value_from_datadict(self, data, files, name):         value = data.get(name)         return {             '1': none, none: none, 'none': none, 'none': none, 'null': none,             '2': true, true: true, 'true': true,             '3': false, 'false': false, false: false,         }.get(value, unset)  class unsetornullbooleanfield(nullbooleanfield):      widget = unsetornullbooleanselect      def to_python(self, value):         if value in (true, 'true', 'true', '1'):             return true         elif value in (false, 'false', 'false', '0'):             return false         elif value in (none, 'none', 'none', 'null'):             return none         else:             return unset 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -