There is an undocumented _meta API that is widely used in Django to learn about models. It stores your model parameters by type and provides about two dozen methods and attributes for checking your model and fields. You can use it to get all the fields of the model, and then from the fields that you can get the column name, since they define all the business logic:
for field in Model._meta.fields: field.get_attname_column()
This will return a tuple that will contain the name of the attribute (field) in the model and the name of the database column. For the model field foo = models.IntegerField(db_column='bar') this will return ('foo', 'bar') .
Filip dupanoviΔ
source share