0

How can I add a new instance, delete an instance, and update an instance of a model in one views.py ?

Currently I have views.py that looks like this:

class BHA_UpdateView(UpdateView):
    template_name = 'bha_test.html'
    model = BHA_List
    pk_url_kwarg = 'pk_alt'
    fields = '__all__'

I know that I can update a current model instance using form:

  <form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" class='btn btn-primary' value="Submit">
  </form>

But what if I want to do delete, create, and update on the same page? I'm not sure how to do if I want to add & delete & update a model instance in a single page.

Ideally, if a user clicks on Delete, or Create, Update button on a page, a modal screen will show up, and within that modal screen the user will be able to do whatever thing he wants to do.

What should I do? Is it possible to inherit multiple CBV, like this: class BHA_UpdateView(UpdateView, CreateView, DeleteView):? If not, what should I do?

Eric Kim
  • 2,493
  • 6
  • 33
  • 69
  • Take a look here; you can override a `CreateView` (or `UpdateView`) and perform other actions in `form_valid`: https://stackoverflow.com/questions/32998300/django-createview-how-to-perform-action-upon-save – FlipperPA Jul 27 '18 at 10:47

1 Answers1

0

You can to do it by creating different form and by giving them different name and process those form in view as per name(using if ). Suppose for deleting it will delete the object and redirect to that page and for submit form it will create the object and redirect.

Sam
  • 815
  • 10
  • 23