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

查看:17
本文介绍了如何从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?

注意:我这样做是为了可以对字段进行验证.例如,如果一个 url ,我想检查它是否格式正确.

推荐答案

如果您正在这样做:

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

你不可能得到那个结果.

You could not possibly get that as a result.

相反,您正在这样做:

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天全站免登陆