Skip to Content
Menu
This question has been flagged
1 Reply
1701 Views

Hello, 

I have a long condition which makes it possible to check if a field has been modified in order to call a function :

        if self.half_pension or not self.half_pension or self.half_pension_occasional \
            or not self.half_pension_occasional or self.half_pension_id != self._origin.half_pension_id \
            or self.half_pension_begin_date != self._origin.half_pension_begin_date \
            or self.half_pension_end_date != self._origin.half_pension_end_date \
            or self.half_pension_monday != self._origin.half_pension_monday \
            or self.half_pension_tuesday != self._origin.half_pension_tuesday \
            or self.half_pension_thursday != self._origin.half_pension_thursday \
            or self.half_pension_friday != self._origin.half_pension_friday \
            or self.half_pension_responsible_partner != self._origin.half_pension_responsible_partner:
                monstringxmlhalfpension = self.get_data_xml_for_halfpension(idUsager)
                resp_halfpension = c.service.XmlAjouterUnEvenement(Synchrone=1, donneesXml=monstringxmlhalfpension)

Is there a way to shorten this condition? Thank you


Avatar
Discard
Best Answer

Hi,

you can apply for fields using '[]' like self["my_field"] (use it only if you are sure that my_field exists). So, you can do something like:

keys = ['half_pension_id', 'half_pension_begin_date', 'half_pension_end_date', 'half_pension_monday'] --> all compared keys
for key in keys:
    if self[key] != self._origin[key]:
        break
        # if any key is broken --> break the loop. Condition is not satisfied
else:
    # we come in 'else' only in case loop is done fully
    # extra checks (e.g. whether certain field is not none/false)
    # do my sy stuff


A little hint: in the most cases it easier to work with documents after saving (not in onchange), in that case you work with values in def write. If would be simple:

if values.get("my_field"):
    # then field is changed

Avatar
Discard
Related Posts Replies Views Activity
3
Apr 24
1019
0
May 24
46
1
Apr 24
1826
4
Sep 23
3085
2
Sep 23
5592