Adding a sequence on an existing field

50 views
Skip to first unread message

Maxime Richez

unread,
Jan 31, 2018, 8:51:26 AM1/31/18
to tryton-dev
Hi,

Some questions and explanations of something i thought easy to realize...
I decide to create a custom module to add a sequence on an existing field. In my case, it concerns stock_lot module. There's a field "number" (type char, required=true) that i need to be "sequenced".

So in my module, there are 2 classes: Configuration, ConfigurationSequence to define my sequence and i update the original class "Lot" to define "number" field "readonly", and set sequence on this number field when creating a new lot.

Here's the code: https://pastebin.com/74EXsjT7

In the first time, forgot the __register__ method.

When executing my new module and saving a new lot, this was not working, i got error on "Number" that is required... it seems number field is not updated by my new sequence...

Checking my create method, there's a test "if values.get('number') is None:"...
I discovered this test is not working because value of the field is not None but "" (checking database, related field is defined as "NOT NULL" because of the "required = True" in the original model and then couldn't be None).

Checking many tryton scripts, i saw it was possible to remove the "NOT NULL" through the register method with "table.not_null_action('number', action='remove')"... but not allowed to do this with a custom module because there is a check if the module_name attempting to update fields is the original module.

So, what's the best way to finish my custom module?

- Changing my original test "if values.get('number') is None:" by "if values.get('number') == "":" (and keeping the database unchanged) ?
- Using some sql in the __register__ method to remove the "NOT NULL" on the "number" column ?

Thanks for suggestions :-)

Raimon Esteve

unread,
Jan 31, 2018, 9:15:06 AM1/31/18
to tryto...@googlegroups.com
Hie,


2018-01-31 14:51 GMT+01:00 Maxime Richez <maxime...@gmail.com>:
>
> Hi,
>
> Some questions and explanations of something i thought easy to realize...
> I decide to create a custom module to add a sequence on an existing field. In my case, it concerns stock_lot module. There's a field "number" (type char, required=true) that i need to be "sequenced".
>
> So in my module, there are 2 classes: Configuration, ConfigurationSequence to define my sequence and i update the original class "Lot" to define "number" field "readonly", and set sequence on this number field when creating a new lot.
>
> Here's the code: https://pastebin.com/74EXsjT7

I think the problem is:

cls.number.states['readonly'] = True

Try to remove required states that is not True (and/or invisible in case that you like not show it)


> In the first time, forgot the __register__ method.
>
> When executing my new module and saving a new lot, this was not working, i got error on "Number" that is required... it seems number field is not updated by my new sequence...
>
> Checking my create method, there's a test "if values.get('number') is None:"...
> I discovered this test is not working because value of the field is not None but "" (checking database, related field is defined as "NOT NULL" because of the "required = True" in the original model and then couldn't be None).
>
> Checking many tryton scripts, i saw it was possible to remove the "NOT NULL" through the register method with "table.not_null_action('number', action='remove')"... but not allowed to do this with a custom module because there is a check if the module_name attempting to update fields is the original module.
>
> So, what's the best way to finish my custom module?
>
> - Changing my original test "if values.get('number') is None:" by "if values.get('number') == "":" (and keeping the database unchanged) ?
> - Using some sql in the __register__ method to remove the "NOT NULL" on the "number" column ?

Cédric Krier

unread,
Jan 31, 2018, 9:20:06 AM1/31/18
to tryton-dev
On 2018-01-31 05:51, Maxime Richez wrote:
> So, what's the best way to finish my custom module?
>
> - Changing my original test "if values.get('number') is None:" by "if values.get('number') == "":" (and keeping the database unchanged) ?

Use a test like this:

if not values.get('number'):


In Tryton required means not False for Python so it includes None,
empty string, empty list, empty dictionary etc.

> - Using some sql in the __register__ method to remove the "NOT NULL" on the "number" column ?

The constraint on removing "NOT NULL" only by the original module is
there on purpose because once a field is required all depending code
will not manage the case if it is empty. So we must ensure that there
will be always a value.
Also it is the reason why we must be careful when setting a field
required.

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Sergi Almacellas Abellana

unread,
Jan 31, 2018, 9:33:32 AM1/31/18
to tryto...@googlegroups.com
El 31/01/18 a les 14:51, Maxime Richez ha escrit:
> I decide to create a custom module to add a sequence on an existing field. In my case, it concerns stock_lot module. There's a field "number" (type char, required=true) that i need to be "sequenced".

I believe there is a generic need to create lot numbers with a sequence
defined. Indeed nantic has a module which allows to configure the
sequence based on the product or on the category [1].

I will be very happy if we find a generic solution that can be part of
tryton.

> Here's the code: https://pastebin.com/74EXsjT7

We use states to save pyson states which. If you want to make it always
readonly you can do:

@classmethod
def __setup__(cls):
super(Lot, cls).__setup__()
cls.number.readonly = True


[1] https://bitbucket.org/nantic/trytond-stock_lot_sequence/src

--
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

Maxime Richez

unread,
Jan 31, 2018, 10:01:36 AM1/31/18
to tryton-dev
> I believe there is a generic need to create lot numbers with a sequence
> defined. Indeed nantic has a module which allows to configure the
> sequence based on the product or on the category [1].
>
> I will be very happy if we find a generic solution that can be part of
> tryton.

Indeed my solution is simpler (only one sequence).

Maxime Richez

unread,
Jan 31, 2018, 10:04:17 AM1/31/18
to tryton-dev

> Use a test like this:
>
> if not values.get('number'):
> …
Ok, working

> The constraint on removing "NOT NULL" only by the original module is
> there on purpose because once a field is required all depending code
> will not manage the case if it is empty. So we must ensure that there
> will be always a value.
> Also it is the reason why we must be careful when setting a field
> required.
Ok, the above test is enough for me and thus no need to change the required field. Thanks!

Maxime Richez

unread,
Jan 31, 2018, 10:06:10 AM1/31/18
to tryton-dev
> I think the problem is:
>
> cls.number.states['readonly'] = True
>
> Try to remove required states that is not True (and/or invisible in case that you like not show it)
>

Not working because unchanged in the database, but also risky like Cédric said... so i just changed my test ;-) Thanks anyway.
Will have a look...
Reply all
Reply to author
Forward
0 new messages