def form_valid(self, form): # Clear any existing assignments for director in GLDirector.objects.all(): director.regions.clear() # Go through the form data and assign GLDirector to regions for key in form.data: region = key.split("_")[0] if region in WARegion.slug_all(): region_obj = WARegion.objects.get(slug=region) for user in form.data.getlist(key): gldirector = GLDirector.objects.get(user__username=user) gldirector.regions.add(region_obj) # Render the same form but with extra context for the template. context = { "success": True } return render(self.request, "gl_tracking/region_assignment_modal_form.html", context)
def test_slug_all(self): expected_result = list(WARegion.objects.all().values_list("slug", flat=True)) self.assertListEqual(WARegion.slug_all(), expected_result)