I need to get all foreign key names of a single model. For example if a model name is UserProfile
and it has user
, customer
, and comment
as foreign keys means by single code I want to get all foreign key names of the UserProfile
model.
You can find relations like so: UserProfile._meta.get_all_related_objects()
You can iterate through fields on the model directly via UserProfile._meta.fields
You can determine the field type via field.get_internal_type()
for field in UserProfile._meta.fields:
if field.get_internal_type() == 'ForeignKey':
print 'found a foreign key'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With