openwisp / django-netjsonconfig

Configuration manager for embedded devices, implemented as a reusable django-app
http://openwisp.org
Other
194 stars 60 forks source link

[models] Avoid extra query to figure out if fields are changing #78

Closed nemesifier closed 4 years ago

nemesifier commented 6 years ago

in Device.clean and Config.clean we perform an extra query to understand if important fields are being changed and the configuration must be flagged as modified.

We can optimize this and get rid of the extra query.

An option could be to use this third party module: https://github.com/adamhaney/django-ondelta If such an approach could work we could start using it in other openwisp modules as well.

atb00ker commented 6 years ago

Problem faced while implementing ondelta: _state.adding value is always false in the ondelta functions.

def clean(self):
    print(self._state.adding) # Output: True
    ...

def ondelta_[Field](self, old_value, new_value):
    print(self._state.adding) # Output: False | [Expected: True]
    ...

To solve this, i created a new variable customStateAdding in clean(), However if the ondelta() is called before the clean() the code breaks, this might cause unintentional bugs.