I'm doing the following, which works nicely when the user proceeds down the golden path:
class MyUpdate(UpdateView)
# ...
class MyDuplicate(MyUpdate):
def get_context_data(self, **kwargs):
context = super(MyDuplicate, self).get_context_data(**kwargs)
context['action'] = "Duplicate"
return context
# where should I call Klass::duplicate?
def form_valid(self, form):
name = form.instance.full_name
video_url = form.instance.video_url
# This doesn't work because it can result in unhandled uniqueness
# constraint violations.
form.instance = Klass.duplicate(
form.instance,
name,
video_url
)
return super(MyDuplicate, self).form_valid(form)
However, if the user attempts to submit an existing full_name
(which must be unique), then the call to Klass.duplicate
results in an unhandled uniqueness constraint violation.
So, my question is: where should I make the call to Klass.duplicate
(unsets pk, resets other values and then calls save -- elided for brevity) in the UpdateView
lifecycle?
UPDATE:
It looks like overriding post
might be one way to do this. Unfortunately, I can't just pass the form instance along to Klass.duplicate
as its fields are empty by default.