如何为类似于django Model/Field的动态字段指定类型? [英] How to specify typings for dynamic fields similar to django Model/Field?

查看:39
本文介绍了如何为类似于django Model/Field的动态字段指定类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

# field base class
class F:
    pass


# represent integer
class AF(F):
    pass


# represent string
class BF(F):
    pass


class M:
    a = AF()
    b = BF()

    def __init__(self):
        pass  # generate dynamic attrs based on specified static attrs


m = M()
# m.a should have inferred type integer

也许这只是具有所有这些类型提示的IDE(PyCharm)功能,并且此功能很难绑定到Django的 Model 类?

Maybe it is just IDE (PyCharm) feature with all these type hint, and this feature is hard-bind to django's Model class?

推荐答案

Django的Model类具有显式检查字段类型以及用于将这些类型添加到DB的数据库迁移的字段.如果要在python中使用静态类型,请考虑使用 MyPy 来注释函数和变量.

Django's Model class has fields that explicitly check the type of the field as well as database migrations to add those types to the DB. If you want to use static types in python, consider using MyPy to annotate your functions and variables.

这篇关于如何为类似于django Model/Field的动态字段指定类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆