How to monitor the changes in the Field Values of the Django model object using signals

Source: Internet
Author: User

How to monitor the changes in the Field Values of the Django model object using signals

Django Signal System

Django comes with a signal transmission system to help us transmit information in different positions of the framework. in other words, when an event occurs, the signal system allows one or more senders to push notifications or signals to a group of recipients (receivers ). the signal system is particularly useful when multiple codes are related to the same event.

Since it is a signal system, it must include the following elements:

1. Sender-who sent the signal

2. Signal-the sent signal itself

3. Receiver-who sent the signal

The function of the Django signal (Signals) is similar to the action of WordPress. It is used to add a global event broadcast (dispatch) and receive (receive) mechanism for the project. Among them, the flexible use of its built-in Model signal reception function can monitor the changes of most Model Objects (Model instances. Because you do not need to modify the model code, it has the advantage of low coupling for cross-application (App) monitoring.

Basic usage

The topic and reference of the official document on the basic usage of signals have been described in detail. This article only mentions a few key points (Environment: Django 1.8 & Python 3.4 ):

Code Organization

It is officially recommended to add a new signals under the application directory. for more information, see the application configuration section of the official documentation to customize the application configuration (AppConfig), reload the run method of the application configuration class, and callfrom . import signals

Receive Signal

Recommendeddjango.dispatch.receiver This decorator receives signals:

from django.db.models import signalsfrom django.dispatch import receiverfrom students.models import Studentfrom .models import Announcement@receiver(signals.post_save, sender=Student)def welcome_student(instance, created, **kwargs): if created:  Announcement.objects.create(content='Welcome new student ' + instance.name)

From the perspective of code readability, it is recommended that a receiving function do only one thing.

Monitor changes in specific field values

As you can see from the previous Code, by receiving the post_save signal of the model, you can know that an operation has occurred to save the model object, and you can also distinguish whether the model object is created or updated. However, the model signal does not provide the Broadcast Function for specific field value changes, although the signal provides the update_fields parameter, however, it cannot be proved that the field value of the field name in this parameter must have changed. Therefore, we need to use a work und that combines the post_init signal.

For example, an announcement is published when the student name changes.

from django.db.models import signalsfrom django.dispatch import receiverfrom students.models import Studentfrom .models import Announcement@receiver(signals.post_init, sender=Student)def welcome_student(instance, **kwargs): instance.__original_name = instance.name@receiver(signals.post_save, sender=Student)def welcome_student(instance, created, **kwargs): if not created and instance.__original_name != instance.name:  Announcement.objects.create(content=   'Student %s has renamed to %s' % (instance.__original_name, instance.name))

Simply put, when the model broadcasts the post_init signal, the current field value is cached in the model object; when the model broadcasts the post_save (or pre_save, compare the current field value of the model object with the cached field value. If the field value is different, it is deemed that the field value has changed.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.