[Answer]-Customizing django object parameters value output

1๐Ÿ‘

โœ…

what does django do, when i do something like that in template

{{ object.parameter }}

See variables and lookups.

Desired output would of course be value, value, value. I would rather not use some kind of filters for that, because then i would have to resort using IFs in templates or use some kind of template tag filter for every value i print out and that does not feel like a smart thing to do.

You can make a really trivial filter:

@register.filter
def comma_join(values):
    return u', '.join(values)

So simple:

{{ object.parameter|comma_join }}

Why would you want to avoid such a simple solution ?

and that gives me exactly what i want, but i thought there would be some other way doing it โ€“ with some modelfield method or something.

Of course you could also add such a method:

class YourModel(models.Model):
    # ....
    def comma_join_parameter(self):
        return u', '.join(self.parameter)

And use it in your template as such:

{{ object.comma_join_parameter }}
๐Ÿ‘คjpic

Leave a comment