Check if variable exists


Is there a way to check from within a relatorio template (odt in my case), if a variable that I want to access actually exists in my data?

I want to generate the document even if some variables are not provided and just leave a blank there. However, currently I am seeing an error in such cases.


With the “IF” instruction. Look at the Sale Report:

<if test="">
<>: <>

Let me add to maxx contribution, that if the field is depending on another and this is not required you should also test that.

<if test=" and">
<>: <>
1 Like


Thank you both! This helps a lot!


I like the solutions of @maxx and @acaubet and use them a lot, too. But the question refers to

The tags <if> and </if> of the condition usually remove the line in which they are defined.
More precise they remove the surrounding/parent tags in the ODF-XML. And you can not see the XML
when editing with a WYSIWIG editor. If the surrounding tag is a paragraph, it could be deleted.
So writing this inside a paragraph could remove the whole paragraph:

This is my paragraph in the odt <if test="condition"> This text comes if condition is true</if> 

With some tricks <if> and </if> can be used inline. But in cases where I just want to result simple chars or even nothing, I would prefer inline expressions, which are short and handy to use:

< if else ' '>

Results in a blank, if is empty, otherwise results in value of It also tests if field bar is set on model foo and avoids error like this AttributeError: 'NoneType' object has no attribute 'name'.
There is also another way to express the above condition:

< and or ' '>

But I find better to read and understand the first expression, because the variable ( is shown in the beginning of the expression. With one exception, if bar is a required field of the model foo you can also use this simple shortcut:

< or ' '>

A more advance technique is needed, when you produce reports for different sets of module dependencies. It can become very handy to add all the possible extras dependencies together in one report instead of one report for one exact set of modules. But in this case a module, model or field may exist in one setup, but not in another. To handle non existing parts in an inline expression, you can use this, when model foo and field bar are always available, but field buz not:

<getattr(, 'baz', False) or ' '>

Results in a blank, if field baz value is empty or the field is not defined on model foo field bar. Otherwise results in value.

This technique is also needed, if you really need to check if the

But language dependent text as output in inline expressions is not translatable with the Tryton translation system.