Django Signals Example

signals

Have you read the Django Signals Documentation and wondered, “Okay, how the heck do you actually use it? How about an example?” If so, then you’ve come to the right place.

Will demo by example adding signals for pre_save, pre_delete to raise an exception if a READ_ONLY_FILE exists to prevent the DB from changing. Then, add signals for post_save and post_delete to print the change to stdout. Using print as an example, a more useful thing would be to write it to syslog or something – say you want to log every DB change that occurs.

Read more

Django Unit Test with Patch and MagicMock Example

brain_magicmock_confused

Ever had a child that asked, “Why?” to every answer you give. It could have started with your statement, “Children should do what their parents tell them to do.” If you try to answer each “Why?” thoroughly, you must psychoanalyze each answer until eventually you’re describing the meaning of the Universe. If that’s what is seems like when learning python’s “from unittest.mock patch, MagicMock”, you’ve come to the right place.

In this article I’m going to unravel the mystery of patch and MagicMock. If at first it seems you need the meaning of the Universe to get it, don’t worry, it’s not that hard. Once I got over this hurdle, writing unit_tests has become second nature. Read on and you can finally know “Why”.

Read more