Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Form Field for a list of strings

I need a Django Form Field which will take a list of strings.

I'll iterate over this list and create a new model object for each string.

I can't do a Model Multiple Choice Field, because the model objects aren't created until after form submission, and I can't do a Multiple Choice Field, because I need to accept arbitrary strings, not just a series of pre-defined options.

Anyone know how to do this?

like image 908
Clay Wardell Avatar asked Nov 28 '12 21:11

Clay Wardell


2 Answers

Just use a regular text field delimited by commas. After you handle the form submission in the view do a comma string split based on that field. Then iterate over each one creating and saving a new model. Shouldn't be too hard.

like image 99
Robeezy Avatar answered Oct 06 '22 21:10

Robeezy


In my case to process list of strings I used forms.JSONField(decoder="array") in my forms.py in form class

like image 22
Yauheni Leaniuk Avatar answered Oct 06 '22 21:10

Yauheni Leaniuk