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?