Check if variable exists

Hi!

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.

Thanks!
Dom

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

<if test="sale.party.tax_identifier">
<sale.party.tax_identifier.type_string>: <sale.party.tax_identifier.code>
</if>

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="your_model.party and your_model.party.tax_identifier">
<your_model.party.tax_identifier.type_string>: <your_model.party.tax_identifier.code>
</if>
1 Like

Great!

Thank you both! This helps a lot!

Dom

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:

<foo.bar.name if foo.bar else ' '>

Results in a blank, if foo.bar.name is empty, otherwise results in value of foo.bar.name. 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:

<foo.bar and foo.bar.name or ' '>

But I find better to read and understand the first expression, because the variable (foo.bar.name) 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:

<foo.bar.name 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(foo.bar, '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 foo.bar.baz 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.

3 Likes