[Fixed]-Django form – key error

1πŸ‘

id is not in your kwargs, that is causing the KeyError. In fact, you don’t need to load the car like that. Your method can be simplified to this:

from django.views.generic import UpdateView

class CarListFormView(UpdateView):
    model = Car
    form_class = CarListForm
    template_name = 'something/car_form.html'

    def form_valid(self, form):
        car = form.save(commit=False)

        if self.request.user.is_staff:
            car.agency = Agency.objects.get(agency_id=9000)
        else:
            car.agency = self.request.user.agency
        car.save()
        return redirect('car_list')
πŸ‘€Brobin

Leave a comment