New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flagging an AnyBlok field as modified #72

Open
gracinet opened this Issue Nov 16, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@gracinet
Copy link
Contributor

gracinet commented Nov 16, 2018

In some cases, a field might correspond to a mutable Python structure, such as a dict. Changing the contents of that data structure is invisible to SQLAlchemy unless one calls flag_modified().

I'm doing this for example in Anyblok / WMS Base for the flexible JSONB field of Model.Wms.PhysObj.Properties: (taken from https://github.com/AnyBlok/anyblok_wms_base/blob/master/anyblok_wms_base/core/physobj/main.py#L484)

          from sqlalchemy.orm.attributes import flag_modified
          self.flexible[k] = v
          flag_modified(self, '__anyblok_field_flexible')

The problem here is that it makes appicative code (AWB in that case) rely on AnyBlok internals (actual name of the attribute as seen from SQLAlchemy).

Therefore it would be a good thing for AnyBlok to expose its own version of flag_modified, so that I could do simply:

     from anyblok.field import flag_modified
     self.flexible[k] = v
     flag_modified(self, 'flexible')

@jssuzanne jssuzanne self-assigned this Nov 16, 2018

@jssuzanne jssuzanne added this to the 1.0.0 milestone Nov 16, 2018

@jssuzanne

This comment has been minimized.

Copy link
Member

jssuzanne commented Nov 16, 2018

I prefere

self.flag_modified('flexible')

The method on the Model could be

def flag_modified(self, *fields):
    ...

The goal is to keep the same partern as refresh or expire

@gracinet

This comment has been minimized.

Copy link
Contributor Author

gracinet commented Nov 16, 2018

@jssuzanne thanks for your reply. Well that's what I didn't dare to suggest (didn't know about refresh() and expire() methods either)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment