class CustomUser(AbstractUser): # get viewable fields with order. viewable_field_list = ('userType', 'username', 'email', 'companyName', 'nearestStation') editable_field_list = ('username', 'companyName', 'nearestStation') def get_selected_fields(self): """Returns a list of selected field names on the instance.""" fields = [] for vf in self.viewable_field_list: for f in self._meta.fields: fname = f.name if vf != fname: continue # resolve picklists/choices, with get_xyz_display() function get_choice = 'get_'+fname+'_display' if hasattr(self, get_choice): value = getattr(self, get_choice)() else: try: value = getattr(self, fname) except AttributeError: value = None
template
<ul> {% for f in object.get_selected_fields %} <li>{{ f.label|capfirst }} ( {{ f.name }} ) : {{ f.value|escape|urlize }} (editable={{ f.is_editable }})</li> {% endfor %}</ul>
必要に応じて、viewの記述も変更
views.py
class UserDetailView(OnlyYouMixin, DetailView): model = CustomUser