python - django开发中纠结于视图函数与类通用视图
阿神
阿神 2017-04-18 09:50:05
0
4
421

现阶段django开发是用视图函数多呢?还是类通用视图?

看到有人在segmentfault中陈述:视图函数拓展性就没有这么灵活。类通用视图可以继承,可用使用装饰器等好处。

不过我做了一个单表的CURD(增删改查)之后,发现一个问题是。现实开发中有很多复杂的功能是多表操作的,类通用视图处理这些复杂的逻辑时就显得有些疲软了.....

各们Django使用者,有什么好建议,或者是我还没对类通用视图研究不透彻?请大家指点一下....

阿神
阿神

闭关修行中......

reply all(4)
Ty80

If you can develop using cbv (class based view), try not to use fbv (function based view). Django’s development organization is also recommending that everyone use cbv for development. There are many advantages to using cbv. The most obvious one is to improve development efficiency. Basically, a simple addition, deletion, modification, and check does not require writing a few lines of code. You only need to figure out what each view is for and inherit it. There are additional You just need to write a mixin extension yourself. Personally, I feel that the development efficiency has been improved by more than 2 times. So to talk about the disadvantages (of course I think this should be an advantage, forcing you to read the Django source code), it is that you need to understand the functions of all the views that come with Django and what each method of the view is doing. Only by understanding the function of view can we perform appropriate inheritance and write appropriate mixin in some complex operations. When you become proficient in developing with cbv, I believe you will also be more familiar with the source code of Django view.

Recommend a website to assist in learning and developing cbv projects: ccbv. It is very clear to learn cbv with Django source code.

Finally, let me add a library that I extended cbv to use for API services. You can refer to how to write mixins to complete some complex functions. Of course, you are also welcome to use dss to quickly develop data interfaces (no advertising, please be serious):

django simple serializer

Ty80

I don’t understand what you want to ask. Regardless of single table or multiple tables, they are all at the Model level, while view functions and view classes are at the View level. Are there any constraints between them? No

巴扎黑

Class views can share some data, but using ordinary view functions requires writing more code. In fact, no matter what kind of view, multi-table association operations can be done, but class views are generally used for RESTful interfaces.

小葫芦

Try to use class-generic views for large projects

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!