I have the following:
# model
TITLE_CHOICES = (
('mr', 'Mr.'),
('ms', 'Ms.'),
('mrs', 'Mrs.'),
('mis', 'Miss.'),
)
class Client(models.Model):
name_title = models.CharField(max_length=3, choices=TITLE_CHOICES)
first_name = models.CharField(max_length=40)
last_name = models.CharField(max_length=40)
# form
class ClientForm(ModelForm):
class Meta:
class = Client
# view
def client_view(request):
client = Client.object.get(id=1)
clientForm = ClientForm(instance=client)
return render_to_response('client.html',{'client':client,
'clientForm':clientForm}, ...)
# client.html
...
How can I loop through the object client printing out the column name and the value while making sure that if the value is a choice it prints out the human-readable choice value, not the stored value (get_title_display)?
And why is this not eaiser to do in Django? (isn’t this a common thing to want do?)
If I can’t do this I have to go statically through each column and use get_title_display, which means that there is no separation between model and template, which means if I change my model I have to manually update the template(s). This is not good
Try something like:
You can then loop over get_fields in your template for a given object