# Check if variable exists

**URL:** <https://discuss.tryton.org/t/check-if-variable-exists/6104>\
**Category:** Developer\
**Tags:** relatorio\
**Created:** [April 27, 2023, 9:05am UTC](https://discuss.tryton.org/t/check-if-variable-exists/6104 "2023-04-27T09:05:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DomCa](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/da6949/32.png) [@DomCa](https://discuss.tryton.org/u/DomCa)\
**Post date:** [April 27, 2023, 9:05am UTC](https://discuss.tryton.org/t/check-if-variable-exists/6104/1 "2023-04-27T09:05:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)\
**Post date:** [April 27, 2023, 9:13am UTC](https://discuss.tryton.org/t/check-if-variable-exists/6104/2 "2023-04-27T09:13:33Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![acaubet](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/acaubet/32/1493_2.png) [@acaubet](https://discuss.tryton.org/u/acaubet)\
**Post date:** [April 27, 2023, 9:31am UTC](https://discuss.tryton.org/t/check-if-variable-exists/6104/3 "2023-04-27T09:31:39Z")

</div>

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

```auto
<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>

```

---

<div class="post-metadata">

**Author:** ![DomCa](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/da6949/32.png) [@DomCa](https://discuss.tryton.org/u/DomCa)\
**Post date:** [April 27, 2023, 9:49am UTC](https://discuss.tryton.org/t/check-if-variable-exists/6104/4 "2023-04-27T09:49:35Z")

</div>

Great!

Thank you both! This helps a lot!

Dom

---

<div class="post-metadata">

**Author:** ![udono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/udono/32/2114_2.png) [@udono](https://discuss.tryton.org/u/udono)\
**Post date:** [April 28, 2023, 10:16am UTC](https://discuss.tryton.org/t/check-if-variable-exists/6104/5 "2023-04-28T10:16:49Z")

</div>

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

> [@DomCa](#):
>
> some variables are not provided and just leave a blank

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:

```auto
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:

```auto
<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:

```auto
<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](https://peps.python.org/pep-0308/):

```auto
<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:

```auto
<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

> [@DomCa](#):
>
> variable that I want to access actually exists

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