我需要获取单个模型的所有外键名称。例如,如果模型名称为UserProfile
,并且具有user
,,customer
并且comment
作为外键意味着通过单个代码,我想获取该UserProfile
模型的所有外键名称。
您可以找到类似的关系: UserProfile._meta.get_all_related_objects()
您可以直接通过遍历模型上的字段 UserProfile._meta.fields
您可以通过确定字段类型 field.get_internal_type()
for field in UserProfile._meta.fields:
if field.get_internal_type() == 'ForeignKey':
print 'found a foreign key'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句