0

I have an app that has employee profiles and I can add safety courses for each profile. In the app I have a form that uses ModelChoiceField to get select values from the database. In the CreateView it works just fine. I can enter the dates and select the course for the profile and add it to the database. But the problem I'm having is when I update the safety course for the profile. When I update the form it updates the "conducted_date" and the "expiration_date", but the select form doesn't update. When I enter the update form the form doesn't even select the value that it currently has, for example:

If I have a profile for "John Doe" and I added "course1" to his profile, that works fine, but when I go to edit the profile, "course1" isn't selected but rather "course2" is selected. Then when I chose another course like "course3" and press submit, the "conducted_date" and "expiration_date" gets updated but not the course. So what exactly am I doing wrong or what am I missing?

forms.py

class SafetyCourseTakenForm(forms.ModelForm):
    course = forms.ModelChoiceField(queryset=SafetyCourse.objects.all(), empty_label=None)

    class Meta:
        model = SafetyCoursesTaken
        fields = [
            'conducted_date',
            'expiration_date',
        ]

views.py

class AddSafetyCourseTakenView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
    login_url = reverse_lazy('users:login')
    form_class = SafetyCourseTakenForm
    template_name = 'ppm/forms/add-course-taken.html'
    success_url = reverse_lazy('ppm:manage-courses-taken')
    success_message = 'Safety course has been added to profile'

    def form_valid(self, form):
        course_taken = form.save(commit=False)
        course_taken.user = self.request.user
        course_taken.profile_id = self.kwargs['pk']
        course_taken.course_id = form.data['course']
        return super(AddSafetyCourseTakenView, self).form_valid(form)

    # override success_url to redirect to url with primary key
    def get_success_url(self):
        pk = self.kwargs['pk']
        return reverse('ppm:manage-courses-taken', kwargs={'pk': pk})


class UpdateSafetyCourseTakenView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
    login_url = reverse_lazy('users:login')
    model = SafetyCoursesTaken
    form_class = SafetyCourseTakenForm
    template_name = 'ppm/forms/update-course-taken.html'
    success_url = reverse_lazy('ppm:manage-courses-taken')
    success_message = 'Safety course has been updated for profile'

    def get_success_url(self):
        profile = SafetyCoursesTaken.objects.get(id=self.kwargs['pk'])
        return reverse('ppm:manage-courses-taken', kwargs={'pk': profile.profile_id})

I've also tried setting init in my forms.py but that didn't work either.

SafetyCourseTakenForm(forms.ModelForm):
...
...
    def __init__(self, *args, **kwargs):
        super(SafetyCourseTakenForm, self).__init__(*args, **kwargs)
        self.fields['course'].queryset = SafetyCourse.objects.all()

So to reiterate. Why is my UpdateView only updating the 'conducted_date' and 'expiration_date' fields but NOT the 'course' field and why doesn't the select field have the proper form value on the edit form?

Note: If you need more information then provided, please let me know.

1 Answer 1

3

You haven't specified that course is a field associated with the model; the only fields that will be saved are conducted_date and expiration_date. Make sure that course is also in the Meta.fields list.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow that was simple but I was close, I tried a variation of that. I tried to use "course_id" in the Meta.fields list, but that didn't work and I didn't think to just try "course". Much appreciated. Since that works then that also makes "course_taken.course_id = form.data['course']" redundant in my AddSafetyCourseTakenView.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.