def filter_form_field_choices(field, predicate, invert=False):
 if (not callable(predicate)):
  allowed_values = set(predicate)
  def predicate(pair):
   return (pair[0] in allowed_values)
 if invert:
  choices = [pair for pair in field.choices if (not predicate(pair))]
 else:
  choices = [pair for pair in field.choices if predicate(pair)]
 field.choices = field.widget.choices = choices

def filter_form_field_choices ( field , predicate , invert = False ) : if ( not callable ( predicate ) ) : allowed_values = set ( predicate ) def predicate ( pair ) : return ( pair [ 0 ] in allowed_values ) if invert : choices = [ pair for pair in field . choices if ( not predicate ( pair ) ) ] else : choices = [ pair for pair in field . choices if predicate ( pair ) ] field . choices = field . widget . choices = choices
filter choices of a form field and its widget by predicate .