为django模型提供检查便利
django-inspect的Python项目详细描述
为django模型提供检查便利。
功能
便利属性
- 所有字段
- 所有模型字段,包括所有关系(来回)。
- 字段
- 仅本地字段,即任何常规字段和关系(向后除外)。
- 非相关字段
- 基本上与字段相同,但不包括所有关系。
- FK字段
- 本地外键字段。
- 向后fk字段
- 仅向后外键字段。
- 所有字段
- 所有外键字段(前后)。
- m2m_字段
- 本地多对多字段。
- 向后m2m字段
- 只将多个字段向后移。
- 所有m2m字段
- 所有多对多字段(来回)。
分检
关系领域的进一步考察。
检查模型(创建Inspectinstance)后,调用:
inspect.sub_inspect("field")
也支持路径:
inspect.sub_inspect("field.other_field..")
它返回一个新的Inspect实例,其中包含有关 关系字段的目标模型。
安装
可通过pip获得:
$ pip install django-inspect
用法
简单检查
fromdjango.contrib.auth.modelsimportUserfromdjango_inspectimportInspectinspect=Inspect(User)# Using an instance/object is also possible.# user = User()# inspect = Inspect(user)inspect.fields[u'id','password','last_login','is_superuser','username','first_name','last_name','email','is_staff','is_active','date_joined','groups','user_permissions']inspect.non_rel_fields[u'id','password','last_login','is_superuser','username','first_name','last_name','email','is_staff','is_active','date_joined']inspect.m2m_fields['groups','user_permissions']inspect.backwards_fk_fields['logentry_set']
分检
fromdjango.contrib.auth.modelsimportUserfromdjango_inspectimportInspectinspect=Inspect(User)sub_inspect=inspect.sub_inspect("user_permissions")sub_inspect.all_fields[u'id','name','content_type','codename']futher_inspect=sub_inspect.sub_inspect("content_type")futher_inspect.all_fields[u'id','name','app_label','model']# Sub-inspecting by pathsub_inspect=inspect.sub_inspect("user_permissions.content_type")sub_inspect.all_fields[u'id','name','app_label','model']
测试
安装
运行make install以安装开发人员需求。
运行测试
运行make test以运行测试。