# Updating selection data in DictSchema

**URL:** <https://discuss.tryton.org/t/updating-selection-data-in-dictschema/5814>\
**Category:** Developer\
**Created:** [January 11, 2023, 6:26pm UTC](https://discuss.tryton.org/t/updating-selection-data-in-dictschema/5814 "2023-01-11T18:26:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [January 11, 2023, 6:26pm UTC](https://discuss.tryton.org/t/updating-selection-data-in-dictschema/5814/1 "2023-01-11T18:26:53Z")

</div>

I walk into a weird problem when updating the selection data in a `DictSchema` through a wizard. I have a class:

```auto
class WebApiReferenceInformation(DictSchemaMixin, ModelSQL, ModelView):
    "API Reference Information"
    __name__ = 'webapi.api.reference.information'

```

Because of the `DictSchema` several columns are added to the database table. Using the wizard I want to change a existing record, or if it doesn’t exist create that record. Adding new records is not a problem. Updating different data is not a problem.

However when I have a record which is has a `type_` of `Selection` the data in the column `selection` cannot be updated. All other columns of that record can be updated like `selection_sorted` or `name`.

I don’t get an error or warning it just doesn’t do it.

---

<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:** [January 12, 2023, 10:08am UTC](https://discuss.tryton.org/t/updating-selection-data-in-dictschema/5814/2 "2023-01-12T10:08:54Z")

</div>

You will have to debug. What are the data you are saving?

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [January 12, 2023, 10:47am UTC](https://discuss.tryton.org/t/updating-selection-data-in-dictschema/5814/3 "2023-01-12T10:47:22Z")

</div>

I’m searching if the record exists and based on that, update or create a new record.

```auto
pool = Pool()
Information = pool.get('webapi.api.reference.information')

information = Information.search([('name', '=', name)])

if information:
    Information.write(information, {'selection': '1:Hello World'})
else:
    Information.create(.....)

```

Debugging shows that there is something going on in the backend which removes the fields from the query. Below are the database queries.

```auto
DEBUG trytond.backend.postgresql.database b'SELECT "a"."id" FROM "webapi_api_reference_information" AS "a" WHERE (("a"."id" IN (83)))'
----> DEBUG trytond.backend.postgresql.database b'UPDATE "webapi_api_reference_information" AS "a" SET "write_uid" = 6, "write_date" = CURRENT_TIMESTAMP WHERE (("a"."id" IN (83)))'
DEBUG trytond.backend.postgresql.database b'SELECT "a"."id" AS "id", "a"."create_date" AS "create_date", "a"."create_uid" AS "create_uid", "a"."fuzzy" AS "fuzzy", "a"."lang" AS "lang", "a"."module" AS "module", "a"."name" AS "name", "a"."overriding_module" AS "overriding_module", "a"."res_id" AS "res_id", "a"."src" AS "src", "a"."type" AS "type", "a"."value" AS "value", "a"."write_date" AS "write_date", "a"."write_uid" AS "write_uid", CAST(EXTRACT(\'EPOCH\' FROM COALESCE("a"."write_date", "a"."create_date")) AS VARCHAR) AS "_timestamp" FROM "ir_translation" AS "a" WHERE (("a"."lang" = \'nl\') AND ("a"."type" = \'model\') AND ("a"."name" = \'webapi.api.reference.information,selection\') AND ("a"."res_id" IN (83))) ORDER BY "a"."id" ASC'
DEBUG trytond.backend.postgresql.database b'SELECT "a"."write_date" AS "write_date", "a"."selection" AS "selection", "a"."id" AS "id" FROM "webapi_api_reference_information" AS "a" WHERE (("a"."id" IN (83)))'
DEBUG trytond.backend.postgresql.database b'SELECT "a"."id" AS "id", "a"."create_date" AS "create_date", "a"."create_uid" AS "create_uid", "a"."fuzzy" AS "fuzzy", "a"."lang" AS "lang", "a"."module" AS "module", "a"."name" AS "name", "a"."overriding_module" AS "overriding_module", "a"."res_id" AS "res_id", "a"."src" AS "src", "a"."type" AS "type", "a"."value" AS "value", "a"."write_date" AS "write_date", "a"."write_uid" AS "write_uid", CAST(EXTRACT(\'EPOCH\' FROM COALESCE("a"."write_date", "a"."create_date")) AS VARCHAR) AS "_timestamp" FROM "ir_translation" AS "a" WHERE (("a"."lang" = \'en\') AND ("a"."type" = \'model\') AND ("a"."name" = \'webapi.api.reference.information,selection\') AND ("a"."value" != \'\') AND ("a"."value" IS NOT NULL) AND ("a"."res_id" IN (83)) AND (("a"."fuzzy" = false) OR ("a"."fuzzy" IS NULL))) ORDER BY "a"."id" ASC'
DEBUG trytond.backend.postgresql.database b'SELECT "a"."id" FROM "ir_translation" AS "a" WHERE (("a"."id" IN (90734)))'
----> DEBUG trytond.backend.postgresql.database b'UPDATE "ir_translation" AS "a" SET "write_uid" = 6, "write_date" = CURRENT_TIMESTAMP, "src" =\'1:Hello World\', "value" = NULL, "fuzzy" = false WHERE (("a"."id" IN (90734)))'
DEBUG trytond.backend.postgresql.database b'SELECT "a"."name" AS "name", "a"."write_uid" AS "write_uid", "a"."src" AS "src", "a"."overriding_module" AS "overriding_module", "a"."write_date" AS "write_date", "a"."id" AS "id", "a"."create_date" AS "create_date", "a"."lang" AS "lang", "a"."module" AS "module", "a"."res_id" AS "res_id", "a"."value" AS "value", "a"."fuzzy" AS "fuzzy", "a"."create_uid" AS "create_uid", "a"."type" AS "type" FROM "ir_translation" AS "a" WHERE (("a"."id" IN (90734)))'
DEBUG trytond.backend.postgresql.database b'SELECT "a"."write_date" AS "write_date", "a"."selection" AS "selection", "a"."id" AS "id" FROM "webapi_api_reference_information" AS "a" WHERE (("a"."id" IN (83)))'
DEBUG trytond.backend.postgresql.database b'SELECT "a"."id" AS "id", "a"."create_date" AS "create_date", "a"."create_uid" AS "create_uid", "a"."fuzzy" AS "fuzzy", "a"."lang" AS "lang", "a"."module" AS "module", "a"."name" AS "name", "a"."overriding_module" AS "overriding_module", "a"."res_id" AS "res_id", "a"."src" AS "src", "a"."type" AS "type", "a"."value" AS "value", "a"."write_date" AS "write_date", "a"."write_uid" AS "write_uid", CAST(EXTRACT(\'EPOCH\' FROM COALESCE("a"."write_date", "a"."create_date")) AS VARCHAR) AS "_timestamp" FROM "ir_translation" AS "a" WHERE (("a"."lang" = \'nl\') AND ("a"."type" = \'model\') AND ("a"."name" = \'webapi.api.reference.information,selection\') AND ("a"."value" != \'\') AND ("a"."value" IS NOT NULL) AND ("a"."res_id" IN (83)) AND (("a"."fuzzy" = false) OR ("a"."fuzzy" IS NULL))) ORDER BY "a"."id" ASC'
DEBUG trytond.backend.postgresql.database b'SELECT "a"."id" FROM "ir_session_wizard" AS "a" WHERE (("a"."id" IN (69149)))'
.....

```

In the first update query the selection data is missing. Using for example `selection_sorted` the query looks like:

```auto
DEBUG trytond.backend.postgresql.database b'UPDATE "webapi_api_reference_information" AS "a" SET "write_uid" = 6, "write_date" = CURRENT_TIMESTAMP, "selection_sorted" = true WHERE (("a"."id" IN (83)))'

```

I also discovered that changing the `string` also cannot be updated, but the translations does.

---

<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:** [January 12, 2023, 11:10am UTC](https://discuss.tryton.org/t/updating-selection-data-in-dictschema/5814/4 "2023-01-12T11:10:09Z")

</div>

This is a translated field so you must update for all the languages.  
Indeed I guess you are writing with a contextual language that is not the default language of Tryton. That’s why the column in the main table is not updated.  
After that I do not know why the value is not stored in the translation but only the source.

Indeed trying to simulate the form behavior in a wizard is a very hard work most of the time.  
**Use the form, Luke** 😉

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [January 12, 2023, 11:32am UTC](https://discuss.tryton.org/t/updating-selection-data-in-dictschema/5814/5 "2023-01-12T11:32:56Z")

</div>

> [@ced](#):
>
> **Use the form, Luke** 😉

In this case that isn’t possible 😢. The wizard will fetch the data from a website API, does some operations on it and then pushes it into the database. The user cannot do much about it.

> [@ced](#):
>
> This is a translated field so you must update for all the languages.  
> Indeed I guess you are writing with a contextual language that is not the default language of Tryton. That’s why the column in the main table is not updated.

Indeed I’m not using the default language of Tryton. Moving the query into a new `Transaction` with the default Tryton language fixes it. I can now change the data on the website and then execute the wizard which updates the data correctly.

So to make it complete:

```auto
with Transaction().set_context({'language': 'en'}):
    pool = Pool()
    Information = pool.get('webapi.api.reference.information')

    information = Information.search([('name', '=', name)])

    if information:
        Information.write(information, {'selection': <selection_data>})
    else:
        Information.create(.....)

```

---

<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:** [February 11, 2023, 11:33am UTC](https://discuss.tryton.org/t/updating-selection-data-in-dictschema/5814/6 "2023-02-11T11:33:23Z")

</div>

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