Django Class Based View for both Create and Update

后端 未结 7 813
有刺的猬
有刺的猬 2020-12-13 18:46

Say I want to create a Class Based View which both updates and creates an object. From a previous question I worked out I

7条回答
  •  一整个雨季
    2020-12-13 19:14

    Why do you need to handle both create and update by a single View? It's much simpler to have two separate views, each inheriting from its respective generic view class. They can share the same form and template if you wish, and they're most likely served from different URLs, so I don't see what would you get by making it into a single view.

    So: use two views, one inheriting from CreateView and the other from UpdateView. These handle pretty much everything you might need, while the second approach would require you to reinvent the wheel yourself. If cases when you have some common "housekeeping" code that is used both when creating or updating objects, the option of using a mixin, or you can perhaps create your own view that covers both use cases, inheriting from both CreateView and UpdateView.

提交回复
热议问题