I have a model containing a ManyToMany field to a table "Tags". Since this table could be huge, I don't want to display a select in the form, but a coma separated list of tags, provided by a charfield (I suppose).
On the saving, I would split the list by their comma and then add them one by one (using get_or_create). I already did that.
But when I want to change data, instead of having a list of tags, I have a list of IDs.
How can I display a list of comma separated tags? Do I have to create a new specific Field for that ? or is there already something that do what I'm looking for?
Thanks for your help!
You'll want to create a custom widget (I'm not aware of a built-in widget that will do exactly what you want). The most useful examples would probably be the widgets that come with Django (in forms/widgets.py). You can also see an example of creating a custom widget here.
I did a little fiddling, and after adapting the built-in
Input
widget this is what I came up with; I did some testing and it works for me:Note that in this example, the widget is hardcoded to use a hypothetical
Tag
model, and it's just using thestr()
method of each object as what will show up in the comma-separated list. You'll probably want to change these to fit your use. Also, I had this directly in forms/widgets.py, so if you put it somewhere else (like you probably should), you'll need to import a few of the things I used.Once you have that created, you can specify it as the widget for your ModelMultipleChoiceField in your form, like so: