[Answer]-Genericizing an iterative view model

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

👤Cheng

Leave a comment