From ac2cc8ec5f08c4daed8810d2fc286ad460cd5d11 Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Thu, 17 Aug 2017 20:42:47 -0400 Subject: [PATCH] Assign user field during Timer instance save. --- core/forms.py | 10 ++++++++++ core/views.py | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/core/forms.py b/core/forms.py index b9bca607..20ee2660 100644 --- a/core/forms.py +++ b/core/forms.py @@ -75,6 +75,16 @@ class TimerForm(forms.ModelForm): model = Timer fields = ['name'] + def __init__(self, *args, **kwargs): + self.user = kwargs.pop('user') + super(TimerForm, self).__init__(*args, **kwargs) + + def save(self, commit=True): + instance = super(TimerForm, self).save(commit=False) + instance.user = self.user + instance.save() + return instance + class TummyTimeForm(forms.ModelForm): class Meta: diff --git a/core/views.py b/core/views.py index bba56012..5cfb01f6 100644 --- a/core/views.py +++ b/core/views.py @@ -147,6 +147,11 @@ class TimerAdd(PermissionRequiredMixin, CreateView): permission_required = ('core.add_timer',) form_class = TimerForm + def get_form_kwargs(self): + kwargs = super(TimerAdd, self).get_form_kwargs() + kwargs.update({'user': self.request.user}) + return kwargs + def get_success_url(self): if resolve(self.request.POST['success_url']).url_name: url = self.request.POST['success_url']