如何从django中的db模型获取字段类型的字符串 [英] how to get field type string from db model in django

查看:91
本文介绍了如何从django中的db模型获取字段类型的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做以下操作:

model._meta.get_field('g').get_internal_type

哪个返回以下内容:

<bound method URLField.get_internal_type of <django.db.models.fields.URLField: g>>

我只想知道这个字段是URLField。如何从这个输出中提取出来?

I only want the know that this field is "URLField" . How do I extract that from this output?

注意:我正在这样做,以便我可以在字段上进行验证。例如,如果一个网址,我想检查它是否形成良好。

推荐答案

如果你这样做:

model._meta.get_field('g').get_internal_type()

你不可能得到结果。

相反,你这样做:

model._meta.get_field('g').get_internal_type

此处所述,不会调用该方法,它只是将方法称为绑定方法对象。返回值不是绑定方法对象的一部分,它是在方法调用时由方法创建的。所以你必须叫它。所以你需要括号。

Which, as explained here, does not call the method, it just refers to the method as a bound method object. The return value is not part of that bound method object, it's created by the method when the method is called. So, you have to call it. So you need the parentheses.

这篇关于如何从django中的db模型获取字段类型的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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