From 229e08df8e7454bbc7fcd545349bb6802e841db5 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Mon, 19 Oct 2020 10:14:35 +0100 Subject: fixed issue of not saving many-to-many field value --- ctrack/register/admin.py | 1 + ctrack/register/forms.py | 18 ++++++++++++------ ctrack/register/views.py | 4 ++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/ctrack/register/admin.py b/ctrack/register/admin.py index 16e20b3..bcd27da 100644 --- a/ctrack/register/admin.py +++ b/ctrack/register/admin.py @@ -16,6 +16,7 @@ class EngagementEventTypeAdmin(admin.ModelAdmin): class SingleDateTimeEventAdmin(admin.ModelAdmin): model = SingleDateTimeEvent + list_display = ("type_descriptor", "short_description", "datetime", "user", "created_date") admin.site.register(EngagementEvent, EngagementEventAdmin) diff --git a/ctrack/register/forms.py b/ctrack/register/forms.py index 39900e6..3253ed5 100644 --- a/ctrack/register/forms.py +++ b/ctrack/register/forms.py @@ -4,6 +4,7 @@ from django import forms from django.core.exceptions import ValidationError from django.shortcuts import get_object_or_404 from django.urls import reverse +from django.utils.safestring import mark_safe from ctrack.caf.models import CAF from ctrack.organisations.models import Organisation, Person @@ -31,6 +32,9 @@ class CreateSimpleDateTimeEventForm(forms.ModelForm): "location", "comments", ] + widgets = { + "participants": forms.CheckboxSelectMultiple() + } def __init__(self, *args, **kwargs): self.event_type = None @@ -44,7 +48,8 @@ class CreateSimpleDateTimeEventForm(forms.ModelForm): if self.org_slug: org = Organisation.objects.get(slug=self.org_slug) self.fields["participants"].queryset = org.get_people() - self.fields["participants"].help_text = f"Click to select participants from {org}." + self.fields["participants"].help_text = mark_safe(f"Click to select participants from {org}. IMPORTANT:" \ + f"You must select at least one participant.") if self.event_type: self.fields["type_descriptor"].initial = self.event_type else: @@ -61,11 +66,12 @@ class CreateSimpleDateTimeEventForm(forms.ModelForm): raise ValidationError("Requested response cannot be before date.") return cleaned_data - def save(self, **kwargs): - form = super().save(commit=False) - form.user = self.user - form.save() - return form + def save(self, commit=True, **kwargs): + new_event = super().save(commit=False) + new_event.user = self.user + new_event.save() + self.save_m2m() + return new_event class CAFSingleDateEventForm(forms.ModelForm): diff --git a/ctrack/register/views.py b/ctrack/register/views.py index 07673b9..db018b2 100644 --- a/ctrack/register/views.py +++ b/ctrack/register/views.py @@ -77,6 +77,10 @@ class SingleDateTimeEventCreate(FormView): form_class = CreateSimpleDateTimeEventForm success_url = reverse_lazy("organisations:list") + def get_success_url(self): + self.success_url = reverse_lazy("organisations:detail", args=[self.kwargs["org_slug"]]) + return super().get_success_url() + def get_context_data(self, **kwargs): context = super().get_context_data() if self.kwargs.get("org_slug"): -- cgit v1.2.3