views:

21

answers:

1

I have ForeignKey in my Django model which can be null.

group = models.ForeignKey(Group, null = True, blank = True)

In case of null value assigned I want to render some specific text in templates (eg. "No grooup assigned"). I use default filter and it's OK except that I repeat this code in various templates. I am looking for solution that will alllow me to assign some display value for None to a FK from some model globally.

Like:

group = models.ForeignKey(Group, null = True, blank = True, display_if_none='No group assigned') 

Is it possible?

A: 

As this is really 'presentation logic' - it belongs best in the templates, or possibly the view.

If you are worried about repeating the logic among many templates, there are several approaches you can take. One possibility is to write a simple custom template tag that would return either a the 'no group assigned', or whatever would be appropriate to display when there is a groups assigned (perhaps the group name, with or without a hyperlink to the group detail page).

Another approach might be to use the include tag, and include a small template which essentially does the same thing as the template tag.

Chris Lawlor