A django model mixin that makes it easy to react to field value changes on models. Supports an API similar to the model clean method.
Given that I have the model
class MyModel(models.Model):
mai_field = models.CharField()
other_field = models.BooleanField()
And I want to be notified when mai_field's value is changed and
persisted I would simply need to modify my model to include a
ondelta_mai_field
method.
from ondelta.models import OnDeltaMixin
class MyModel(OnDeltaMixin):
mai_field = models.CharField()
other_field = models.BooleanField()
def ondelta_mai_field(self, old_value, new_value):
print("mai field had the value of {}".format(old_value))
print("but after we called save it had the value of {}".format(new_value))
This is the easiest method to watch a single field for changes, but
what if we want to perform an action that has an aggregate view
of all of the fields that were changed? OnDeltaMixin
provides an
ondelta_all
method for these cases; it is only called once for
each save.
from ondelta.models import OnDeltaMixin
class MyModel(OnDeltaMixin):
mai_field = models.CharField()
other_field = models.BooleanField()
ondelta_all(self, fields_changed):
if 'mai_field' in fields_changed and 'other_field' in fields_changed:
print("Both fields changed during this save!")
Some field types are not supported: ManyToManyField
, reverse ManyToManyField
,
reverse ForeignKey
, and reverse OneToOneField
relations. If you create an
ondelta_field_name
method for one of these fields, it will not be called when
that field is changed.
I like to help people as much as possible who are using my libraries, the easiest way to get my attention is to tweet @adamhaney or open an issue. As long as I'm able I'll help with any issues you have.
Bumps ipython from 2.4.1 to 7.16.3.
Sourced from ipython's releases.
7.9.0
No release notes provided.
7.8.0
No release notes provided.
7.7.0
No release notes provided.
7.6.1
No release notes provided.
7.6.0
No release notes provided.
7.5.0
No release notes provided.
7.4.0
No release notes provided.
7.3.0
No release notes provided.
7.2.0
No release notes provided.
7.1.1
No release notes provided.
7.1.0
No release notes provided.
7.0.1
No release notes provided.
7.0.0
No release notes provided.
7.0.0-doc
No release notes provided.
7.0.0rc1
No release notes provided.
7.0.0b1
No release notes provided.
6.2.1
No release notes provided.
... (truncated)
d43c7c7
release 7.16.35fa1e40
Merge pull request from GHSA-pq7m-3gw7-gq5x8df8971
back to dev9f477b7
release 7.16.2138f266
bring back release helper from master branch5aa3634
Merge pull request #13341 from meeseeksmachine/auto-backport-of-pr-13335-on-7...bcae8e0
Backport PR #13335: What's new 7.16.28fcdcd3
Pin Jedi to <0.17.2.2486838
release 7.16.120bdc6f
fix conda buildDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps django from 1.11.23 to 2.2.24.
2da029d
[2.2.x] Bumped version for 2.2.24 release.f27c38a
[2.2.x] Fixed CVE-2021-33571 -- Prevented leading zeros in IPv4 addresses.053cc95
[2.2.x] Fixed CVE-2021-33203 -- Fixed potential path-traversal via admindocs'...6229d87
[2.2.x] Confirmed release date for Django 2.2.24.f163ad5
[2.2.x] Added stub release notes and date for Django 2.2.24.bed1755
[2.2.x] Changed IRC references to Libera.Chat.63f0d7a
[2.2.x] Refs #32718 -- Fixed file_storage.test_generate_filename and model_fi...5fe4970
[2.2.x] Post-release version bump.61f814f
[2.2.x] Bumped version for 2.2.23 release.b8ecb06
[2.2.x] Fixed #32718 -- Relaxed file name validation in FileField.Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
ondelta_[Field] is giving incorrect value for _state.adding
def clean(self):
print(self._state.adding) # Output: True
...
def ondelta_name(self, old_value, new_value):
print(self._state.adding) # Output: False
...
Expected Output: True
Here are the models:
``` class Foo(OnDeltaModel): name = models.CharField(max_length=50) is_public = models.BooleanField(default=False)
class Bar(models.Model): foo = models.ForeignKey(models.Model, related_name='items'): ```
And the traceback:
```
foo.save() Traceback (most recent call last): File "[...]python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_code exec code_obj in self.user_global_ns, self.user_ns File "
", line 1, in c.save() File "[...]python2.7/site-packages/ondelta/models.py", line 75, in save self._ondelta_dispatch_notifications() File "[...]python2.7/site-packages/ondelta/models.py", line 55, in _ondelta_dispatch_notifications setattr(self._ondelta_shadow, field, changes['new']) File "[...]python2.7/site-packages/django/db/models/fields/related.py", line 404, in set manager.add(*value) TypeError: add() argument after * must be a sequence, not RelatedManager ```
It happens because 'items' returns as a field name in the _ondelta_fields_to_watch() method of Foo, although it's not a field. django.db.models.options.get_all_field_names() includes reverse relation names.
Serial Entrepreneur, VP of Engineering @invisible-tech, former CTO @eat-on-the-house, @bellhops,
GitHub Repository