Post to UpdateView and populate the template.

I am trying to initialise UpdateView with a POST of a record’s ‘pk’, rather than the default method of a GET request with the ‘pk’ appended to the url.

Despite all of my efforts, I only ever end up with an invalid ‘This field is required.’ form.

I have tried the following test code (which deliberately omits the usual error handing):

class UserDataUpdateView(UpdateView):
    model = UserData
    template_name = 'dashboard/data_create_update.html'
    fields = ['data']

    def get_object(self, queryset=None):
        pk = self.request.POST.get("pk")
        queryset = self.get_queryset()
        queryset = queryset.filter(pk=pk)
        obj = queryset.get()
        return obj

    def post(self, request, **kwargs):
        self.object = self.get_object()
        return self.render_to_response(self.get_context_data())

My question is, how can I customise UpdateView so that I can POST the ‘pk’ of a record, pre-populate the form with the existing record data, and then update the data as required?

Don’t bother trying this with any of the Django generic CBVs. You’re trying to get that view to do two different things from the same type of request.

The POST to a CBV is used to submit the form. If you change it to initialize the form, then how are you going to submit the form data?

If you really need to initialize the form with POST data, then you’re going to need to build your form such that a submit is going to submit the form to a different url - a different view.