0👍
✅
You can pass the model as a parameter to the function:
def CustomerTable(anymodel):
and then you can build the json iterating on the fields provided by
anymodel._meta.get_all_field_names()
however, the mapping of the fields would not be exact (fieldname_id for foreign keys).
So, as already mentioned in Cheng’s answer, it’s better to use
anymodel._meta.get_fields()
1👍
check out this post: Django: Get list of model fields?
There are more methods you can call, so take a look at the official documentation: https://docs.djangoproject.com/en/1.8/ref/models/meta/#retrieving-all-field-instances-of-a-model
- [Answer]-Request.POST empty for Content-Type: multipart/form-data; boundary=xYzZY
- [Answer]-Django multiple Forms in a single Form instance,validation error
- [Answer]-Django – Getting form field by string var
- [Answer]-Django CreateView does not save object
Source:stackexchange.com