I have a Django (1.8) model that has some class based generic views: list, update, delete, detail, create. https://docs.djangoproject.com/en/1.8/ref/class-based-views/
On the detail or list view, I have a button that I want to do this:
- Create a copy of the object
- Load the data into a form and display for the user to edit/save the new object (could use the existing update or create views, or a new one?)
I can clone the model with this info: How do I clone a Django model instance object and save it to the database?
But I can't make the leap to get this done by starting at a view and ending at a form with the copied object data.
Thanks!
partial views.py
class List(ListView):
model = Announcement
template_name = 'announcements/list.html'
class Create(CreateView):
model = Announcement
form_class = AnnouncementForm
template_name = 'announcements/form.html'
def form_valid(self, form):
data = form.save(commit=False)
data.author = self.request.user
data.save()
return super(Create, self).form_valid(form)
class Update(UpdateView):
model = Announcement
form_class = AnnouncementForm
template_name = 'announcements/form_update.html'
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(Update, self).dispatch(*args, **kwargs)
partial forms.py
class AnnouncementForm(forms.ModelForm):
class Meta:
model = Announcement
exclude = ['author']
partial list.html
{% for object in object_list %}
<p>object.title</p>
<a class="btn btn-danger" href="{% url 'announcements:delete' object.id %}" role="button">Delete</a>
<a class="btn btn-info" href="{% url 'announcements:update' object.id %}" role="button">Edit</a>
<a class="btn btn-primary" href="" role="button">Copy</a>
{% endfor %}
What I hit the "Copy" button in list.html, I want to duplicate the object and open the new duplicate in a form for editing.