31👍
✅
There is an undocumented _meta
API that’s widely used throughout Django for introspecting models. It stores your model options on the type and provides about two dozen methods and attributes to inspect your model and it’s fields. You can use it to get all the model fields and then from the fields you can get the column name, since they specify all the business logic:
for field in Model._meta.fields:
field.get_attname_column()
This will return a tuple that will contain the attribute (field) name on the model and the DB column name. For a model field foo = models.IntegerField(db_column='bar')
, this would return ('foo', 'bar')
.
0👍
You can do a query on the database using raw SQL and use the answer to this question:
How to get field names when running plain sql query in django.
- Migrate datetime w. timezone in PostgreSQL to UTC timezone to use Django 1.4
- How to serve static files to AWS when deploying Django app (`python manage.py collectstatic` didn't work)?
Source:stackexchange.com