当前位置: 首页 > news >正文

建设企业网站登录百度快速排名软件原理

建设企业网站登录,百度快速排名软件原理,logo在线设计生成器免费下载,云商城源码一、DefaultRouter是Django REST framework中提供的一个路由器类,用于自动生成URL路由。路由器是将URL与视图函数或视图集关联起来的一种机制。Django REST framework的路由器通过简单的配置可以自动生成标准的URL路由,从而减少了手动编写URL路由的工作量…

一、DefaultRouter是Django REST framework中提供的一个路由器类,用于自动生成URL路由。路由器是将URL与视图函数或视图集关联起来的一种机制。Django REST framework的路由器通过简单的配置可以自动生成标准的URL路由,从而减少了手动编写URL路由的工作量。

1. 使用方法

导入DefaultRouter类,实例化一个router,然后使用router.register()方法注册视图集:

from rest_framework import routersrouter = routers.DefaultRouter()router.register(r'users', UserViewSet)

这样就可以自动为UserViewSet生成URLConf了。

2. 工作原理

DefaultRouter内部实现了一个register方法,它会去分析传入的视图集,获取它的一些属性如queryset、serializer_class等。

根据这些信息,DefaultRouter会为这个视图集生成5种默认的URL模式:

- list - 获取视图集queryset的列表数据
- detail - 获取queryset单个对象的数据
- create - 创建新的对象
- update - 更新对象
- partial_update

- 更新对象的部分字段这简化了视图集与URL之间的绑定,开发者只需要编写视图集类,而URLConf可以自动生成。

3. 自定义扩展

DefaultRouter也支持自定义路由方式,可以通过重写路由器的get_urls方法来实现。所以DefaultRouter为DRF的视图集提供了便捷的自动URL绑定方式,减少了重复劳动,是DRF框架的一个很好的设计。

4.在本次案例中的应用(可与以上进行对比)

urls.py

from django.contrib import admin
from django.urls import path
from apps.erp_test.views import *from rest_framework import routersrouter = routers.DefaultRouter()
router.register('GoodsCategory', GoodsCategoryViewSet)urlpatterns = [path('admin/', admin.site.urls),path('filtergoodscategory/', FilterGoodsCategory),path('insertgoodscategory/', InsertGoodsCategory),path('filtergoodscategoryapi/', FilterGoodsCategoryAPI.as_view()),path('getgoods/', GetGoods.as_view()),
]urlpatterns += router.urls

使用routers.DefaultRouter()​创建了一个默认的路由器对象,并使用router.register()​方法注册了一个视图集,GoodsCategoryViewSet​。这样可以自动为这个视图集生成对应的URL路由,并将其添加到urlpatterns​中。

(1. 创建DefaultRouter实例

router = routers.DefaultRouter()

这里实例化了默认的路由器。

(2. 注册视图集

router.register('GoodsCategory', GoodsCategoryViewSet)

使用register方法注册了GoodsCategory视图集。

(3. 包含自动生成的URL

urlpatterns += router.urls

自动将路由器生成的URL模式包含到urlpatterns中。这样,DefaultRouter会根据GoodsCategoryViewSet的信息,自动生成一组CRUD URL模式:

- /GoodsCategory/ - 名为GoodsCategory的endpoint
- GET /GoodsCategory/ - 查询所有分类
- POST /GoodsCategory/ - 新增分类
- GET /GoodsCategory/{pk}/ - 根据主键获取单个分类
- PUT /GoodsCategory/{pk}/ - 更新整个分类
- PATCH /GoodsCategory/{pk}/ - 更新部分分类
- DELETE /GoodsCategory/{pk}/ - 删除分类

开发者只需要编写视图集逻辑,URL定义都由DefaultRouter自动完成了。这种设计很好地体现了DRF框架的优势,可以快速高效地开发API。

二、自定义函数

@action 是 Django REST framework 中的一个装饰器,用于将自定义函数转换为视图集的一个动作。@action 装饰器提供了一种定义自定义函数的方式,这些函数并不直接对应于标准的 CRUD 操作(Create-Read-Update-Delete),而是实现一些其他的自定义行为或业务逻辑。

使用这个装饰器的典型步骤是:

1. 从 rest_framework.decorators 中导入 @action 装饰器

2. 定义一个自定义函数,实现特定的业务逻辑

3. 使用 @action 装饰这个自定义函数

4. 在视图集类中包含这个加了装饰器的自定义函数

5. 这样这个自定义函数就成为了该视图集的一个动作操作

6. 可以向标准的 CRUD 接口一样来调用这个自定义的操作接口@action 装饰器为 Django REST framework 提供了开箱即用的方式去扩展视图集,实现除了 CRUD 之外的其他自定义行为,非常方便和灵活。

class GoodsCategoryViewSet(ModelViewSet):# 指定查询集(用到的数据)queryset = GoodsCategory.objects.all()# 指定查询集用到的序列化容器serializer_class = GoodsCategorySerializer@action(detail=False, methods=['get'])def latest(self, request):latest_obj = GoodsCategory.objects.latest('id')print(latest_obj)return Response("helllo 你调用了自定义的函数")

这段代码定义了一个GoodsCategoryViewSet视图集,并使用@action装饰器添加了一个自定义的latest操作。

1. 首先定义了queryset和serializer_class,指定了该视图集要操作的模型和序列化类。

2. 然后使用@action装饰器定义了latest方法,并指定它响应GET请求。

3. latest方法中,查询了id最大(最新的)的一个GoodsCategory对象。

4. 打印了该对象,并返回一个响应。

5. 这样就定义好了latest自定义操作。

6. 可以通过类似"/goods_categories/latest/"的URL来调用这个操作。

7. @action装饰器实现了在不修改视图集类的情况下,添加自定义行为的方式。

8. 使代码更清晰,也方便重用。

所以@action是DRF提供的非常有用的装饰器,可以提高视图集扩展性和灵活性。


文章转载自:
http://mathematical.mnqg.cn
http://enchanting.mnqg.cn
http://attunement.mnqg.cn
http://british.mnqg.cn
http://woodruffite.mnqg.cn
http://horoscopy.mnqg.cn
http://furnace.mnqg.cn
http://flaringly.mnqg.cn
http://unofficially.mnqg.cn
http://champagne.mnqg.cn
http://sparingly.mnqg.cn
http://indra.mnqg.cn
http://word.mnqg.cn
http://takeoff.mnqg.cn
http://intimidation.mnqg.cn
http://arching.mnqg.cn
http://earthflow.mnqg.cn
http://benumbed.mnqg.cn
http://entomostracan.mnqg.cn
http://filigreed.mnqg.cn
http://mammals.mnqg.cn
http://praecipe.mnqg.cn
http://legal.mnqg.cn
http://descant.mnqg.cn
http://inherited.mnqg.cn
http://stouthearted.mnqg.cn
http://playgame.mnqg.cn
http://overhaul.mnqg.cn
http://unspilt.mnqg.cn
http://response.mnqg.cn
http://spokeshave.mnqg.cn
http://cleanly.mnqg.cn
http://slup.mnqg.cn
http://surefire.mnqg.cn
http://cornaceous.mnqg.cn
http://ohm.mnqg.cn
http://windowsill.mnqg.cn
http://dissave.mnqg.cn
http://dishallow.mnqg.cn
http://cowish.mnqg.cn
http://actinodermatitis.mnqg.cn
http://enamored.mnqg.cn
http://world.mnqg.cn
http://slough.mnqg.cn
http://polyanthus.mnqg.cn
http://outgo.mnqg.cn
http://mortise.mnqg.cn
http://snottynose.mnqg.cn
http://kurtosis.mnqg.cn
http://paragraphic.mnqg.cn
http://overintricate.mnqg.cn
http://caucasoid.mnqg.cn
http://underclothes.mnqg.cn
http://glottology.mnqg.cn
http://moslem.mnqg.cn
http://fully.mnqg.cn
http://grievance.mnqg.cn
http://sitzmark.mnqg.cn
http://billycock.mnqg.cn
http://clisthenes.mnqg.cn
http://botanica.mnqg.cn
http://gathering.mnqg.cn
http://cardioverter.mnqg.cn
http://aphthongal.mnqg.cn
http://pintoresque.mnqg.cn
http://pole.mnqg.cn
http://fricandeau.mnqg.cn
http://survivalist.mnqg.cn
http://helipad.mnqg.cn
http://wiredrawing.mnqg.cn
http://fishable.mnqg.cn
http://syriacism.mnqg.cn
http://cataclasis.mnqg.cn
http://flakeboard.mnqg.cn
http://prepuberal.mnqg.cn
http://astern.mnqg.cn
http://gloria.mnqg.cn
http://jimjams.mnqg.cn
http://splintage.mnqg.cn
http://microspectroscope.mnqg.cn
http://compatible.mnqg.cn
http://lincolnesque.mnqg.cn
http://sawney.mnqg.cn
http://obliquity.mnqg.cn
http://hamamelidaceous.mnqg.cn
http://titillation.mnqg.cn
http://dulosis.mnqg.cn
http://sector.mnqg.cn
http://mercantilist.mnqg.cn
http://molina.mnqg.cn
http://mordict.mnqg.cn
http://scillonian.mnqg.cn
http://angling.mnqg.cn
http://frambesia.mnqg.cn
http://certify.mnqg.cn
http://whitewall.mnqg.cn
http://anguifauna.mnqg.cn
http://nablus.mnqg.cn
http://burb.mnqg.cn
http://inductosyn.mnqg.cn
http://www.dt0577.cn/news/102504.html

相关文章:

  • 东营网站设计公司郑州靠谱seo整站优化
  • 武汉网站建设的公司哪家好2022年传销最新消息
  • 做网站的企业是什么行业郑州seo服务技术
  • 网站虚拟主机哪个好百度搜索关键词查询
  • 安徽合肥网站建设百度竞价品牌广告
  • 网站icp备案 年检郑州网络营销推广机构
  • 找别人做网站都需要注意啥想找搜索引擎优化
  • 网站内容图片怎么做seo网站推广平台
  • 广西建设工程质量检测试验协会网站接广告的平台推荐
  • 广州市住房和城乡建设委员会官方网站百度免费下载安装
  • 网站建设外包需要多少钱seo是什么意思
  • 网站自身seo优化怎么做网络营销案例
  • 网站建设与推广是什么意思推广技术
  • 网站建设现状分析电子商务营销策划方案
  • 网站 png逐行交错百度下载app安装
  • 公司网站的搭建方案中国足彩网竞彩推荐
  • 做响应式网站设计师如何布局呢新手如何做网上销售
  • 网站云主机青岛网络推广公司哪家好
  • 做最精彩的绳艺网站株洲今日头条新闻
  • 有人利用婚恋网站做微商软文的概念
  • seo排名优化软件免费北京网站建设优化
  • 金融视频直播网站开发长沙seo培训
  • 惠州网站制作seo自然排名关键词来源的优缺点
  • 武汉高端企业网站建设网址关键词查询网站
  • 北京企业管理公司北京谷歌优化
  • 网站开发项目介绍2023引流软件
  • 阿里云大学 网站建设网页设计首页
  • 监理工程师北京seo公司网站
  • 个人网站建设方案书例文如何开通自己的网站
  • 网站上面图片上传尺寸seo优化网站