Giter Site home page Giter Site logo

adamhaney / django-ondelta Goto Github PK

View Code? Open in Web Editor NEW
51.0 6.0 10.0 42 KB

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.

License: MIT License

Python 100.00%

django-ondelta's Introduction

django-ondelta

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.

Quick Start

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!")

Unsupported Field Types

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.

Help

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.

django-ondelta's People

Contributors

adamhaney avatar dependabot[bot] avatar fpruitt avatar hwkns avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

django-ondelta's Issues

License?

What is the license for django-ondelta? I see the LICENSE file says MIT, but in setup.py, it says LGPL.

Django 1.9 Support

A change in Django 1.9, officially released today, has caused importing OnDeltaMixin to fail. Just wanted to leave an example stack trace here while I work on a possible solution.

File "/var/www/bellhops/kinetic/hophr/models.py", line 11, in <module>
    from ondelta.models import OnDeltaMixin
  File "/home/vagrant/.venv/lib/python3.4/site-packages/ondelta/models.py", line 12, in <module>
    class OnDeltaMixin(models.Model):
  File "/home/vagrant/.venv/lib/python3.4/site-packages/django/db/models/base.py", line 94, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/vagrant/.venv/lib/python3.4/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
    self.check_apps_ready()
  File "/home/vagrant/.venv/lib/python3.4/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")

TypeError: add() argument after * must be a sequence, not RelatedManager

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 "<ipython-input-10-6970514bfc33>", line 1, in <module>
    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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.