On Wed, 2009-01-07 at 10:38 -0800, sagi s wrote:
> Can anyone suggest a way to customize model field display?

Use auxillary functions. Model fields carry no implicit "display"
concept with them, since they represent the data object, separate from
the presentation.

> 
> For example if I have a model called Course with fields n_girls and
> n_students,
> 
> if for a particular record n_girls = 4 and n_students = 10, I want to
> display for n_girls: "40% (4)" instead of just "4".
> 
> Obviously I can do this in the template but there are multiple fields
> I want to customize and the lack of switch/elsif statement in the
> templates would make for a deeply nested structure so I'd rather do it
> upstream in the model.

Write some model methods that return the appropriate string format of
the given field. Or write some filters that accept a model object as the
input value and format it appropriately. Using template filters for
presentation changes always feels pretty natural to me. Model methods
work, but, as noted above, my inclination is always to use models as the
presentation-agnostic data object and set things up so that I can
control presentation indepedently of the data itself.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to