M2M with Intermediary on django form not saving full selection, attrs for multiple select needed?

I had an issue with my m2m form and an error thankfully this helped:

Class-based views for M2M relationship with intermediate model

Though I did use it verbatim and I must of done something wrong, because in my form now if I select two of my locations it does not save and keeps the original selection. If I select one or the other it works. I am thinking I need somewhere in my forms.py or my views.py to state it is a multiple select box? Though I figured it would inherit that from the model it doesn't seem to be. I can shift select it in the form but saving the object again results in only one location being saved not both. No idea how to track this down....

Essentially this form is letting a user create an employee and then assign what location of a business that employee works at. When I edit the with multiple locations (say that I assigned in the admin page) then the employee (the provider) has both selected on the form that loads to edit. But saving it again only selects one. I know that my form_valid code is deleting everything but it never realizes more than one was there/ or selected in the formgui. I am thinking I am missing an attribute somewhere?

views.py:

class ProviderCreateView(CreateView):     model = Provider     form_class = ProviderForm     template_name = 'ipaswdb/provider/provider_form.html'     success_url = 'ipaswdb/provider/'      def form_valid(self, form):         self.object = form.save(commit=False)         ProviderLocations.objects.filter(provider=self.object).delete()         #i believe only one is always coming back.   Even if i shift-select two.         for group_location in form.cleaned_data['group_locations']:             location = ProviderLocations()             location.provider = self.object             location.group_location = group_location             location.save()         return super(ModelFormMixin, self).form_valid(form)  class ProviderUpdateView(UpdateView):     model = Provider     form_class = ProviderForm     template_name = 'ipaswdb/provider/provider_form.html'     success_url = 'ipaswdb/provider/'       def form_valid(self, form):         self.object = form.save(commit=False)         ProviderLocations.objects.filter(provider=self.object).delete()          for group_location in form.cleaned_data['group_locations']:             location = ProviderLocations()             location.provider = self.object             location.group_location = group_location             location.save()         return super(ModelFormMixin, self).form_valid(form) 

My template uses the default (I don't do anything to this field in the forms.py)

   <div class="col-2">       {{ form.group_locations.errors }}      <label>     Group Locations           {{ form.group_locations }} 

Replay

Category: python Time: 2016-07-29 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.132 (s). 12 q(s)