[Django]-How to get the name of the Foreign Key tables for a model instance in Django?

3👍

You can query the _meta (Options) API of the related model for the db_table attribute:

related_field.related_model._meta.db_table

For example:

for field in instance._meta.concrete_fields:
    if isinstance(field, models.ForeignKey):
        print(field.related_model._meta.db_table)

Leave a comment