django:在哪里可以找到模型字段类型转换为 postgres

django: where to find models field type translation to postgres

我正在尝试将给定数据库的 table 与应用程序的模型定义进行比较。

class SewManAbstract(models.Model):
   """
   """
   display_name = models.CharField(max_length=255, blank=True)
   code = models.CharField(
       max_length=100, blank=True,
       help_text='txt')
   length = models.FloatField(_("length"), null=True, help_text='input')

当我直接查询数据库的元数据时,我得到

meta = ([u'display_name', u'character varying(255)', 
     u'code', u'character varying(100)', 
     u'length', u'double precision']) 

当我使用 django 的内置功能时

FieldNameType = collections.namedtuple(
    "FieldNameType", 'field_name, field_type')
tmp = []
for field in model._meta.local_fields:
    fn = FieldNameType(
        field.name, model._meta.
            get_field(field.name).get_internal_type())
    tmp.append(fn)

我明白了

[FieldNameType(field_name='display_name', field_type=u'CharField'), 
FieldNameType(field_name='code', field_type=u'CharField'),
FieldNameType(field_name='length', field_type=u'FloatField')]

比较两个不同的输出并不容易。所以我的问题真的是:我在哪里可以找到 django 的模型字段类型到相应的 postgres 字段类型的内部翻译?

您可以在 this moduledjango.db.backends.postgresql_psycopg2.base)中找到此翻译 DatabaseWrapper class的data_types属性。