If the formatting could be applied to different classes, I'd use a  
filter. If the formatting only makes sense for Houses, I'd make it a  
method on House.

James

On 14/02/2007, at 9:51 PM, Jonathan Ballet wrote:

>
> Hello everybody,
>
> some days ago, my co-worker did some things which keep bothering me.
>
> He has a simple model, which represents, as it's name point out,
> err ... houses :
>
> class House(models.Model):
>     price = models.FloatField(max_digits=10, decimal_places=2)
>     to_buy = models.BooleanField(default=True) # False = to rent
>     nb_rooms = models.PositiveIntegerField(null=True)
>     ## etc.
>
> Then, he has a simple view, which displays a list of all houses from
> the database, with a formatted title.
> The title is a mix of some fields of a house object : for ex., if the
> number of rooms is not available, it is not displayed, etc.
> (currently, it's a 30 lines function).
> He ends up with a title which looks like this : "House for rent, with
> 4 rooms - 1000€" or "House to buy - 100000€"
>
> So, the question is : what is the "best" way of rendering this title
> in a template.
>
> We found multiple solutions (looping over each "house"s of the list) :
>  * a get_title() method in the House class which is used like that :
> {{ house.get_title }}
>  * a format_title filter : {{ house|format_title }}
>  * adding an additionnal "title" attribute for every house objects,
> which is set in the view by a function (how ugly)
>  * ... something else ?
>
> To my mind, I think using a filter is better, since it allows you to
> redefine another format of title, without changing anything into your
> model (since the application should be reusable for other projects).
> However, I'm not sure if this is the "best" way to do this ...
>
> Any comments welcome !
>
>  - Jonathan
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to