I need to forrward this data as python list to django, not as string. How to create list, just to input data separated with commas?
class CreateForm(forms.Form):
list = forms.CharField(label='Input names (separate with commas): ')
I need to forrward this data as python list to django, not as string. How to create list, just to input data separated with commas?
class CreateForm(forms.Form):
list = forms.CharField(label='Input names (separate with commas): ')
You can define your own field to make it a comma separated field. This then looks like:
# app_name/fields.py
class CommaSeparatedField(forms.CharField):
def to_python(self, value):
if value in self.empty_values:
return self.empty_value
value = str(value).split(',')
if self.strip:
value = [s.strip() for s in value]
return value
def prepare_value(self, value):
if value is None:
return None
return ', '.join([str(s) for s in value])
and then use it in the form with:
# app_name/forms.py
from app_name.fields import CommaSeparatedField
class CreateForm(forms.Form):
list = CommaSeparatedField(label='Input names (separate with commas): ')
Django will then return a list of strings in the form.cleaned_data in case the form is valid.