Assign user field during Timer instance save.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-17 20:42:47 -04:00
parent 1b015d4dde
commit ac2cc8ec5f
2 changed files with 15 additions and 0 deletions

View File

@ -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:

View File

@ -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']