Giter Site home page Giter Site logo

bentleycook / django-ondelta Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adamhaney/django-ondelta

0.0 2.0 0.0 416 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

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", mai_field
        print "but by the time we called save it had the value of", new_value

This is the easiest method to watch a single field for changes but what about 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 which 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 fields_changed['other_field']['old'] == True:
            print "other field was true and is now", fields_changed['other_field']['new']
            print "We also have access to", fields_changed['mai_field']['old']

Design Considerations

All effort to be made not to over notify on changes. Any comparison problems should fail in a way that does NOT duplication notifications. Field comparisons should also not cause other problems in the model (for example causing a child to be unable to persist).

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

Watchers

 avatar  avatar

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.