0
我得到我的模式是這樣的字段列表:如何獲取Django模型的數據類型列表?
c = Client.objects.all().first()
fields = c._meta.get_all_field_names()
我怎樣才能得到這些字段的數據類型(CharField,INT,等等)的列表?有沒有比在每個領域查找get_internal_type
屬性更簡單的方法?
謝謝!
我得到我的模式是這樣的字段列表:如何獲取Django模型的數據類型列表?
c = Client.objects.all().first()
fields = c._meta.get_all_field_names()
我怎樣才能得到這些字段的數據類型(CharField,INT,等等)的列表?有沒有比在每個領域查找get_internal_type
屬性更簡單的方法?
謝謝!
您可以使用:
raw_list = c._meta.get_fields_with_model()
當你raw_list = c._meta.get_fields_with_model()
raw_list包含類似:
((<django.db.models.fields.AutoField: id>, None), (<django.db.models.fields.TextField: signature>, None) etc...
爲了得到一個「解析」列表中只包含我們可以做的數據類型的名稱:
[item[0].__class__.__name__ for item in raw_list._meta.get_fields_with_model()]
或使用get_internal_type:
[item[0].get_internal_type() for item in raw_list._meta.get_fields_with_model()]
在這兩種方式,你會得到這樣的列表:
['AutoField', 'TextField', 'TextField', 'FloatField', 'CharField', 'BooleanField', 'IntegerField', 'ImageField', 'BooleanField'...
只需將代碼:
raw_list = c._meta.get_fields_with_model()
parsed_list = [item[0].__class__.__name__ for item in raw_list._meta.get_fields_with_model()]