4

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:

  1. Create a copy of the object
  2. 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.

Community
  • 1
  • 1
43Tesseracts
  • 4,617
  • 8
  • 48
  • 94
  • Welcome to Stack Overflow! This question is somewhat broad; can you include information about a specific problem that you're running into? Please see [ask] – jtbandes Jul 23 '15 at 05:53
  • Thanks, I added some specific code, does that narrow it down enough? – 43Tesseracts Jul 23 '15 at 06:11

2 Answers2

2

It think I figured it out!

urls.py

#eg: myapp/5/copy/
#where 5 is the item I want to copy
url(r'^(?P<id>[0-9]+)/copy/$', views.item_copy, name='item_copy'),

views.py:

def item_copy(request, id):
    new_item = get_object_or_404(MyModel, pk = id)
    new_item.pk = None #autogen a new pk (item_id)
    new_item.name = "Copy of " + new_item.name #need to change uniques

    form =  MyForm(request.POST or None, instance = new_item)

    if form.is_valid():
        form.save()
        return redirect('my_view')

    context = {
        "form": form,
        #other context
    } 

    return render(request, "form.html", context)
43Tesseracts
  • 4,617
  • 8
  • 48
  • 94
0
class CopyView(ManageAnnouncement, DeleteView):

    def dispatch(self, *args, **kwargs):
        obj = self.get_object()
        obj.pk = None
        copy = obj.save()
        return HttpResponseRedirect('/announcement/edit/%s' %(copy.id))
        # Change the redirect page to the one you need.

I have inherited a base class called ManageAnnouncement. You can put methods or variables common to multiple classes in an abstract base class and inherit it in adding, editing deleting, copying etc, so that code gets 'dry'.

Vinayak Kaniyarakkal
  • 1,110
  • 17
  • 23