# Best-practice how to use UserWarnings?

**URL:** <https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640>\
**Category:** Developer\
**Created:** [September 20, 2021, 9:09am UTC](https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640 "2021-09-20T09:09:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [September 20, 2021, 9:09am UTC](https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640/1 "2021-09-20T09:09:19Z")

</div>

I’d like to learn for to use UserWarning properly. Implementations in official modules - which I took as an example - use quite different approaches, the [documentation](https://docs.tryton.org/projects/server/en/latest/topics/user_errors_warnings.html) shows yet another approach.

My questions are:

- It is required or recommended to define a custom sub-class of `UserWarning`?

- How to form the `warning_name` (aka `key`)? Which values should go into the key? Quite different forms used in the code:

---

<div class="post-metadata">

**Author:** ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)\
**Post date:** [September 20, 2021, 9:17am UTC](https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640/2 "2021-09-20T09:17:16Z")

</div>

> [@htgoebel](#):
>
> It is required or recommended to define a custom sub-class of `UserWarning`?

Yes, so you will be able to catch such specific warning from the calling code

> [@htgoebel](#):
>
> How to form the `warning_name` (aka `key`)? Which values should go into the key?

The key is used to identify the warning. It depends a little bit on the kind of warning and if it’s releated to some records but it should be used to identify the warning from the user prespective.

For example, if the warning is related to some wrong data on some records the key should be unique per set of records. This way the warning is raised for each records that do not satifsty the condition.

On the other hand, if the warning is generic, you can include just a name to it’s key.

The main idea here is that each unique key can be skipped by the user in case the warning is reraised.

---

<div class="post-metadata">

**Author:** ![JCavallo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@JCavallo](https://discuss.tryton.org/u/JCavallo)\
**Post date:** [September 20, 2021, 9:33am UTC](https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640/3 "2021-09-20T09:33:36Z")

</div>

Hello,

The thing to be wary about for the key is that the transaction which initiated the warning will be rollbacked, and re-executed “from scratch” if the user clicks “Yes”.

So any `id` that was allocated will be lost, and new `id`s will be used.

Bottom line is, if you use an `id` in your warning key, make sure it refers a record that existed before the current transaction, or else you will be stuck in a loop.

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [September 20, 2021, 10:10am UTC](https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640/4 "2021-09-20T10:10:17Z")

</div>

Thanks so far.

> [@pokoli](#):
>
> For example, if the warning is related to some wrong data on some records the key should be unique per set of records. This way the warning is raised for each records that do not satifsty the condition.

So the key would be e.g.

```
key = Warning.format('mymodule.check_conflict',
          [record, conflicting_data_1, conflicting_data_2])

```

or:

```
key = 'mymodule.check_conflict,%s,%s,%s' % (
          (record, conflicting_data_1, conflicting_data_2)

```

Right? Which one is to prefer?

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [September 20, 2021, 11:44am UTC](https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640/5 "2021-09-20T11:44:17Z")

</div>

For my use-case I decided to use a readable key, so in the future the database could be cleaned up if the warnings are not longer relevant (e.g. since the record is “closed”, “stopped”, “paid”)

```auto
key = 'mymodule.check_conflict,%s,%s,%s' % (
          (record, conflicting_data_1, conflicting_data_2)

```

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [September 20, 2021, 12:00pm UTC](https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640/6 "2021-09-20T12:00:47Z")

</div>

The warning records are automatically deleted after usage so there is no point to clean them.  
But using `Warning.format` ensure a standardization and a reproducibility of warning names across systems.

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [September 20, 2021, 12:41pm UTC](https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640/7 "2021-09-20T12:41:20Z")

</div>

> [@ced](#):
>
> The warning records are automatically deleted after usage

Thanks. I didn’t know (obviously ;-). This is good news.

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [October 20, 2021, 12:42pm UTC](https://discuss.tryton.org/t/best-practice-how-to-use-userwarnings/4640/8 "2021-10-20T12:42:17Z")

</div>

Closed on October 20, 2021, 12:42pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
