Django打字提示包
django-hint的Python项目详细描述
Django_hint
是一个模块,可以帮助您键入提示,提示您的django项目使用不同的ide。
Notice: Python3.6 or later is required for this module
安装
您可以使用pip
来安装django_提示
pip3 install django_hint
用法
可以使用django_hint
来提示以下用例类型,以帮助IDE识别变量的类型。
- 数据库查询集
- wsgirequest
- django rest framework令牌身份验证
- 模型类
另外,所有来自typing
模块的本地python类型提示,如List
、Union
、Optional
等,都可以从django_hint
数据库查询集
它用于提示变量是一个包含多个对象的QuerySet
对象,我们将确定这些对象的性质。
您需要将其提示给QueryType
,并在[]
中传递对象类型。示例:
fromdjango_hintimportQueryTypesample_query:QueryType[SampleModel]=SampleModel.objects.filter(name='sample')
变量sample_query
将被视为QuerySet
。在对象之间循环时,每个对象都将被视为SampleModel
wsgirequest
它用于提示视图的request
参数的性质(基于函数和类)。
request
将被视为附加有user
变量的HttpRequest
。示例:
fromdjango_hintimportRequestTypedefsample_view(request:RequestType):ifrequest.user.is_authenticated:print(request.POST.get('data'))
django rest framework令牌身份验证
如果使用Django-Rest-Framework
的令牌身份验证,则请求对象将具有user
变量和rest_framework.authtoken.models.Token
实例的auth
变量。DRFTokenRequestType
将提示这两个变量的ide。
fromdjango_hintimportDRFTokenRequestTypedefsample_view(request:DRFTokenRequestType):print(request.auth.key)
模型类
django向Model
实例添加了一些属性,这些属性在models.Model
中不可用,在ide中也不可用。
最显著的属性是Manager
,可以通过名为objects
的属性访问该属性。
要在IDE中包含这些属性,您必须将模型扩展到StandardModelType
类和django_hint
类,并像使用任何其他模型类一样使用它。
请注意,StandardModeltype
不会对数据库产生任何影响,也不会对makemigrations
命令进行新的迁移。
fromdjango.dbimportmodelsfromdjango_hintimportStandardModelTypeclassSampleModel(models.Model,StandardModelType):"""Just like any other model"""pass