So I want to create a method in my model class (I'm using a DetailView for the view) that returns a pre-approved set of field values and user friendly names that I can call in my template. This method has to check if the field values aren't the default field values for each approved field in the model and then return a list with the field value/name pairs to my template.
The problem is that I can't find a way of extracting field.value()
and field.default
as shown in my psuedocode below:
def displayFields(self):
approvedFields = [ ('field1','Field One'),
('field2','Field Two'),
('field3','Field Three')
]
resultFieldPairs = []
for fieldName in approvedFields:
field = self._meta.get_field_by_name(fieldName[0])
if field.value() != field.default:
resultFieldPairs.append(tuple([fieldName[1], field.value()]))
return resultFieldPairs
I can see from the errors I'm getting that self._meta.get_field_by_name()
returns a RelatedObject, but the Django docs don't seem to be clear on this object's attributes and methods.
Please help.
field.value()
do you meangetattr(self, field.name)
– Stuart Leigh