how to get foregine key field from models 1

how to get foregine key field from models

>>> SubCategory._meta.get_field('category').remote_field.model.__name__ 
>>> 'Category'

Here is what the above code is Doing:
1. We’re getting the SubCategory model.
2. We’re getting the category field from the SubCategory model.
3. We’re getting the remote_field from the category field.
4. We’re getting the model from the remote_field.
5. We’re getting the name of the model.

Similar Posts