如何在 Django 中检查 reverse_lazy() 对象的类型

How to check type of reverse_lazy() object in django

我想做以下事情:

u=reverse_lazy('xyz')
isinstance(u,str) # this is false since u is an object.

type(u) # <class 'django.utils.functional.lazy.<locals>.__proxy__'>

isinstance(u,<class 'django.utils.functional.lazy.<locals>.__proxy__'>) # doesnt work

u.__dict__你会得到以下命令:

{'_proxy____args': ('xyz',), '_proxy____kw': {}}

有了它,你可以得到初始参数,然后得到它们的类型。

u=reverse_lazy('xyz')

initial_arg = u._proxy____args[0]

print(initial_arg)
# 'xyz'

print(type(initial_arg))
# <class 'str'>