简体   繁体   中英

Repeating a single field multiple times in a django form

Caveat: I'm aware of fieldsets, but remain unconvinced they are needed for such a simple idea.

I have a simple table creation form that from a given dataset allows a user to extract certain columns:

class TableBuildingForm(forms.Form):
    data_set = forms.ChoiceField(choices=DATASETS,required=True,label="Initial object")
    col1 = forms.CharField(label='Column 1', max_length=100, required=False)
    col2 = forms.CharField(label='Column 2', max_length=100, required=False)
    col3 = forms.CharField(label='Column 3', max_length=100, required=False)
    col4 = forms.CharField(label='Column 4', max_length=100, required=False)
    col5 = forms.CharField(label='Column 5', max_length=100, required=False)
    sort_by = forms.CharField(label='Sort by', max_length=100, required=False)

Then when processing the view I do:

def custom_table(request):
    # if this is a POST request we need to process the form data
    rows = []
    columns = []

    if request.method == 'POST':
      form = forms.TableBuildingForm(request.POST)
      if form.is_valid():
        sort_by = form.cleaned_data['sort_by']
        columns = [ col for col in [
                    form.cleaned_data['col1'],
                    form.cleaned_data['col2'],
                    ... etc ...
                  ]

Ignoring the front-end aspects of a dynamic form (which isn't difficult), the immediate problem is what if a user wants more than 5 columns, say 6, or 9 or 42.

Well, every answer I've seen suggests formsets. But for this use case that means making a form with one single field - col1 - which just seems overly complex.

What I'd like is something like:

class TableBuildingForm(forms.Form):
    data_set = forms.ChoiceField(choices=DATASETS,required=True,label="Initial object")
    columns = forms.CharField(label='Column 1', max_length=100, required=False)
    sort_by = forms.CharField(label='Sort by', max_length=100, required=False)

With the corresponding:

def custom_table(request):
    # if this is a POST request we need to process the form data
    rows = []
    columns = []

    if request.method == 'POST':
      form = forms.TableBuildingForm(request.POST)
      if form.is_valid():
        sort_by = form.cleaned_data['sort_by']
        columns = form.cleaned_data['columns']

Is there a simple way of declaring that a field may be repeated in a django form * , or if thats not the case is there a way of catching all the returned data pre-cleaning/validation to get all of the columns?

* I'm not expecting django to build the front-end for me, I can do that. I'm just looking for a way for django to not throw a complaint if multiple fields are returned

Yes, there is one other way to do this.

In stead of declaring all your fields in the form like this -

class TableBuildingForm(forms.Form):
    data_set = forms.ChoiceField(choices=DATASETS,required=True,label="Initial object")
    col1 = forms.CharField(label='Column 1', max_length=100, required=False)
    col2 = forms.CharField(label='Column 2', max_length=100, required=False)
    col3 = forms.CharField(label='Column 3', max_length=100, required=False)
    col4 = forms.CharField(label='Column 4', max_length=100, required=False)
    col5 = forms.CharField(label='Column 5', max_length=100, required=False)
    sort_by = forms.CharField(label='Sort by', max_length=100, required=False)

declare all your form fields inside the __init__ method of the form. Because __ini__ is called when the form is created in for GET and POST. So when the binding of the values from HTTP post is done, you will have all your dynamic fields populated and bind properly -

class TableBuildingForm(forms.Form):
    data_set = forms.ChoiceField(choices=DATASETS,required=True,label="Initial object")            
    sort_by = forms.CharField(label='Sort by', max_length=100, required=False)

    def __init__(self, data=None, files=None, instance=None, **kwargs):
        super().__init__(data=data, files=files, instance=instance, **kwargs)

        for x in xrange(10): # just a dummy for 10 values
            self.fields['col' + str(x)] = forms.CharField(label='Column ' + str(x), max_length=100, required=False)

Then in the clean method you can get the values -

def clean(self):
    value = self.cleaned_data['field_' + str(0)]

This is how I populate forms which has dynamic meta fields.

EDIT: If a field added with JS

For this to handle, keep another field name count

class TableBuildingForm(forms.Form):
        data_set = forms.ChoiceField(choices=DATASETS,required=True,label="Initial object")            
        sort_by = forms.CharField(label='Sort by', max_length=100, required=False)
        count = forms.HiddenField(required=False)

then update it will the count of the values with JS. Say for example 5;

Then update your init to consider this value -

    def __init__(self, data=None, files=None, instance=None, **kwargs):
        super().__init__(data=data, files=files, instance=instance, **kwargs)
        _count = int(self.cleaned_data['count'])
        for x in xrange(_count ): # get values from count because it will be updated with js
            self.fields['col' + str(x)] = forms.CharField(label='Column ' + str(x), max_length=100, required=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM