How to change multiple selection fields to multiple input fields for the many-to-many case?

When I show ToolBoxEditForm, it uses a multiple select box. But what I want is a form that allows the user to edit each tool that he has in the toolbar as a text field. I cannot figure out how to do this with the many to many field.

class Tool(models.Model): tool_name = models.CharField(unique=True, max_length=200) ...... class ToolBox(models.Model): tools = models.ManyToManyField(Tool,max_length=300) class ToolBoxEditForm (ModelForm): tools = ??? class Meta: model = ToolBox exclude = ('user', 'popularity',) 
+4
source share
2 answers

The sexiest decision

You can use one of the jquery autocomplete tools described here: JQuery autocomplete plugin for Facebook

Then in the form:

 class ToolBoxEditForm (ModelForm): tools = forms.CharField(widget=forms.Textarea, required=False) def clean_tools(self): tool_data = self.cleaned_data.get('tools',None) tools = [] #here, a comma is used a delim, so it not allowed in the tool name. for td in tool_data.split(','): t, _ = Tool.objects.get_or_create(name=td) tools.append(t) return tools class Meta: model = ToolBox exclude = ('user', 'popularity',) 

You will need to figure out how to change JavaScript so that you can introduce new elements (i.e. not just the ones already in the database).

Alternative solution

This is what was created by the built-in form sets , so the Narendra solution will work.

Sort of:

 from django.forms.models import inlineformset_factory def manage_toolbox(request, toolbox_id): toolbox = Toolbox.objects.get(pk=toolbox_id) ToolInlineFormSet = inlineformset_factory(Toolbox, Tool) if request.method == "POST": formset = ToolInlineFormSet(request.POST, request.FILES, instance=toolbox) if formset.is_valid(): formset.save() # Do something. else: formset = ToolInlineFormSet(instance=toolbox) return render_to_response("manage_toolbox.html", { "formset": formset, }) 

Not that this form was just for editing items in a toolbar. If you want the user to be able to edit other aspects of the toolbar - say, its name or description, you would create a separate form and display both of them in <form></form> tags.

+2
source

Iโ€™m not sure, because I havenโ€™t tested it, but the logic is on.

  • Creating a formset for ToolBoxEditForm via formset_factory
  • Change the field type username to CharField
  • Set the number of lines in the form set exactly for the number of Tool objects available in db
  • Pass the initials to the form designer to populate the tool_name text fields.

    # TODO: the following data should be generated dynamically

    initial_data = [{'tool_name': u'first_tool_name ',}, {' tool_name ': u'second_tool_name',}]

    formset = ToolBoxFormSet (extra = 0, initial = initial_data)

Not sure about verification part. Here we put computer_name as the value for the text field. During validation, the form may expect an identifier (because it must be a list). But you can also handle this.

for more information on formet, contact: http://docs.djangoproject.com/en/dev/topics/forms/formsets/

0
source

All Articles