How to make Flask-WTFoms update labels dynamically from list of label names?

前端 未结 2 970
萌比男神i
萌比男神i 2021-01-21 08:55

I use WTForms to define form for data filtering it is defined like this (My goal is to have user specified labels for BooleanFields set, I let each user to name labels for field

2条回答
  •  迷失自我
    2021-01-21 09:00

    The approach that you have taken, calendar_colors_descriptions is assigned in the body of your form class.

    This means that it is only evaluated once - when the forms module is first imported - and so the field label values are fixed until the server is restarted. In effect, the label values are part of the class definition, and so common across all instances of the class.

    This example code is similar to yours;

    import random
    import wtforms
    
    
    def get_labels(labels=None):
            if labels is None:
                labels = ['red', 'amber', 'green']
            # Simulate data changes by shuffling the list.
            random.shuffle(labels)
            return labels
    
    
    class StaticLabelForm(wtforms.Form):
    
        # labels is set when the class is compiled at import time.
        labels = get_labels()
    
        foo = wtforms.BooleanField(labels[0], default=True)
        bar = wtforms.BooleanField(labels[1], default=True)
        baz = wtforms.BooleanField(labels[2], default=True)
    

    Each time we instantiate a new StaticLabelForm, the labels are always the same, because the get_labels function is only called once.

    >>> static1 = StaticLabelForm()
    >>> for field in static1: print(field.label, field)
    ... 
     
     
     
    
    >>> static2 = StaticLabelForm()
    >>> for field in static2: print(field.label, field)
    ... 
     
     
     
    

    We can fix this by passing the label values to the form's __init__ method, and setting them on the fields inside the __init__ method.

    class DynamicLabelForm(wtforms.Form):
    
        # Don't set the labels here
        foo = wtforms.BooleanField(default=True)
        bar = wtforms.BooleanField(default=True)
        baz = wtforms.BooleanField(default=True)
    
        def __init__(self, labels=None, **kwargs):
            super().__init__(**kwargs)
            # super(DynamicLabelForm, self).__init__(**kwargs) for python2!
            if labels is None:
                labels = ['red', 'amber', 'green']
            self['foo'].label = wtforms.Label(self['foo'].id, labels[0])
            self['bar'].label = wtforms.Label(self['bar'].id, labels[1])
            self['baz'].label = wtforms.Label(self['baz'].id, labels[2])
    

    Now the labels are reset on each new form:

    >>> dynamic1 = DynamicLabelForm(labels=get_labels())
    >>> for field in dynamic1: print(field.label, field)
    ... 
     
     
     
    
    >>> dynamic2 = DynamicLabelForm(labels=get_labels())
    >>> for field in dynamic2: print(field.label, field)
    ... 
     
     
     
    

提交回复
热议问题