是否可以使用Python类型提示指定Django QuerySet中的记录类型?像QuerySet[SomeModel]
?如何在Django QuerySet中使用Python类型提示?
例如,我们有型号:
class SomeModel(models.Model):
smth = models.IntegerField()
而且我们想通过这种模式的查询集为PARAM在FUNC:
def somefunc(rows: QuerySet):
pass
但如何在查询集指定的记录类型,如与List[SomeModel]
:
def somefunc(rows: List[SomeModel]):
pass
但用QuerySet?
我希望使用'smth'的类型提示并指定'smth:QuerySet',但是如何在QuerySet中指定记录类型? –
检查这两个问题,他们可能会包括你的答案。 1)http://stackoverflow.com/questions/32557920/what-are-type-hints-in-python-3-5 2)http://stackoverflow.com/questions/35230635/type-hinting-in-python -2 – ALJ
这不是关于如何传入参数的问题。 OP询问如何为只能接受Django QuerySet对象的函数参数指定[PEP 484 type hints](https://www.python.org/dev/peps/pep-0484/)。 –