<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Kevin Stone</title><description>Personal blog for Kevin Stone.</description><link>https://blog.kevinastone.com/</link><item><title>Generate Your Tests</title><link>https://blog.kevinastone.com/generator-tests/</link><guid isPermaLink="true">https://blog.kevinastone.com/generator-tests/</guid><description>When testing your application, you’ll often come across the need to test that
your application behaves correct across a range of inputs or conditions.
Instead of repetitively defining the same test scaffolding, let’s use some
meta-programming to build our tests methods from a list of parameters.
Nose has support for Generator Tests, but they don’t
work with python’s unittest framework. Let’s walk-through how to build our
own test generator that does.</description><pubDate>Mon, 12 Oct 2015 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When testing your application, you’ll often come across the need to test that
your application behaves correct across a range of inputs or conditions.
Instead of repetitively defining the same test scaffolding, let’s use some
meta-programming to build our tests methods from a list of parameters.
&lt;a href=&quot;https://nose.readthedocs.org/en/latest/&quot;&gt;Nose&lt;/a&gt; has support for &lt;a href=&quot;https://nose.readthedocs.org/en/latest/writing_tests.html#test-generators&quot;&gt;Generator Tests&lt;/a&gt;, but they don’t
work with python’s &lt;code&gt;unittest&lt;/code&gt; framework. Let’s walk-through how to build our
own test generator that does.&lt;/p&gt;
&lt;h2 id=&quot;our-test-case&quot;&gt;Our Test Case&lt;/h2&gt;
&lt;p&gt;So let’s write a simple application for demonstration. We want to see if our
input string is alphabetized.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;sorted&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let’s write a unit test case to check our implementation:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; unittest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;unittest&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;abcd&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_is_not_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertFalse&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;zyxw&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can go ahead and run these tests and they should pass:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; nosetests example&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Ran&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; tests&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 0.000s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But as you can see, our two test methods only tested simple examples. What we
really want to test is our function over a wide range of inputs to ensure our
implementation is robust.&lt;/p&gt;
&lt;p&gt;At this point, your thought is usually to iterate over a range of cases in your method, and assert that each of them works as desired:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;unittest&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        inputs &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;a&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;aaaaa&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;ab&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;abcd&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;            self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But you lose the benefit of having a single assertion in your test. You can’t
isolate your test run to a single input. What if your test requires a setup
or teardown after each case? You can’t use all the advantages of the test
framework.&lt;/p&gt;
&lt;p&gt;Let’s say you have this setup and you get bug report that our naive
implementation above doesn’t work correctly for &lt;code&gt;A-Cert&lt;/code&gt; as well as one for &lt;code&gt;iOS&lt;/code&gt;. Since we’re big fans
of TDD, we’ll plug that in our test case of expected inputs:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;alphabetical &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; is_alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; unittest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;unittest&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        inputs &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;a&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;aaaaa&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;ab&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;abcd&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;A-Cert&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;iOS&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;            self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And give it a run:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 34,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_is_alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; False&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; is&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But as you can see, we don’t know which test failed. We should update our
test case to indicate which word failed:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        inputs &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;a&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;aaaaa&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;ab&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;abcd&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;A-Cert&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;iOS&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;            self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 46,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_is_alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;), u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{} was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; A-Cert&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; considered&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we at least know which word failed. But what about &lt;code&gt;iOS&lt;/code&gt;? Because the
test exits on the first failure, we’re unsure if that bug report was valid or
not.&lt;/p&gt;
&lt;p&gt;Before we go fixing our &lt;code&gt;is_alphabetical&lt;/code&gt; implementation to handle the
punctuation and capitalization, let’s figure out how to get both inputs to
fail.&lt;/p&gt;
&lt;p&gt;Instead of looping in our test method, we could instead write separate test methods for each input:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;unittest&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_is_alphabetized_empty&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_is_alphabetized_a&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_is_alphabetized_aaaaa&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;aaaaa&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But boy is that exhausting having to repeat ourselves for each test. Instead,
we’d like to provide the list of inputs as before, but have individual test
methods generated for us. Nose actually supports this out of the box with
&lt;a href=&quot;https://nose.readthedocs.org/en/latest/writing_tests.html#test-generators&quot;&gt;Generator Tests&lt;/a&gt;, but you can’t use them with python
&lt;code&gt;unittest.TestCase&lt;/code&gt; and we’d lose any of help we get using the framework.&lt;/p&gt;
&lt;p&gt;But we can work around that limitation. We just need to define a new test
method on our test class for each input and nose will run them just like any
other test method.&lt;/p&gt;
&lt;p&gt;What we need to do is transform that list of inputs into individual test
methods. Instead of us manually writing all those test methods, what if we
wrote a function that wrote the test class for us?&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;unittest&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; add_methods&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    Take a TestCase and add a test method for each input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;            self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;test_input &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;add_methods&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &apos;a&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &apos;aaaaa&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &apos;ab&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &apos;abcd&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &apos;A-Cert&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &apos;iOS&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Running those tests, we see:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 16,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;), u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{} was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; iOS&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; considered&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Ran&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; tests&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 0.001s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAILED&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (errors=1, &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;failures=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What happened? Shouldn’t there be more tests? Well, we kept clobbering our
test in the klass. The trick is to give each test a unique name.&lt;/p&gt;
&lt;p&gt;Let’s assign the test to the class using &lt;code&gt;setattr&lt;/code&gt; with a custom name:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; add_methods&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    Take a TestCase and add a test method for each input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        test_name &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;test_alphabetical_&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;            self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;        setattr&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now running the tests:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_alphabetical_&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 18,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;), u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{} was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; iOS&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; considered&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_alphabetical_A-Cert&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 18,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;), u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{} was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; iOS&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; considered&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Ran&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 7&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; tests&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 0.001s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAILED&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (failures=7)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Why are all tests failing with &lt;code&gt;iOS&lt;/code&gt;? It should be running with each variant
of our input. Turns out this is due to variable scoping in python. In
python2, there’s only two levels of scoping: function and global. So as we
loop and define &lt;code&gt;test_input&lt;/code&gt;, the value of &lt;code&gt;input&lt;/code&gt; is being updated. When our
test methods are actually evaluated, it’s always equal to the last value
(&lt;code&gt;iOS&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;To fix that, we’ll define yet another function to capture the value of input
(remember there’s function scoping):&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; make_method&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    test_name &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;test_alphabetical_&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; add_methods&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    Take a TestCase and add a test method for each input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        test_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; make_method&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;        setattr&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With these changes, we finally get the results we expected:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_alphabetical_A-Cert&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 14,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;), u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{} was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; A-Cert&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; considered&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_alphabetical_iOS&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 14,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;), u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{} was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; iOS&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; considered&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Ran&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 7&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; tests&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 0.001s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAILED&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (failures=2)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There you go, we are able to generate N test methods to validate our
alphabetical function. Adding a new test is as simple as extending the list
of inputs.&lt;/p&gt;
&lt;h2 id=&quot;improving-the-test-generation&quot;&gt;Improving the Test Generation&lt;/h2&gt;
&lt;p&gt;So we now have the ability to generate tests from an input list, but it
involves a few methods and our test is now deeply nested in a helper factory.
Let’s improve this so we can use this more universally.&lt;/p&gt;
&lt;p&gt;First thing to notice is that &lt;code&gt;add_methods&lt;/code&gt; takes a class as the first
argument. That looks awfully like a decorator. Let’s make a few tweaks so we
can use it to decorate our test class:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; add_methods&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    Take a TestCase and add a test method for each input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; decorator&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            test_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; make_method&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;            setattr&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; klass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; decorator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;@add_methods&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;a&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;aaaaa&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;ab&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;abcd&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;A-Cert&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;iOS&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;unittest&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All we did was wrap our method generation in a &lt;code&gt;decorator&lt;/code&gt; function that takes
an input class, adds the method and returns the class.&lt;/p&gt;
&lt;h3 id=&quot;naming-the-test-functions&quot;&gt;Naming the Test Functions&lt;/h3&gt;
&lt;p&gt;if you inspect the test class, even though we have methods under unique names
based on the input (like &lt;code&gt;test_input_iOS&lt;/code&gt;), we can’t reference those class
names when using nose because they all are still just named &lt;code&gt;test_input&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;ipdb&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; pp IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;test_alphabetical_iOS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;unbound method IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;test_input&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To fix this, we need to set the &lt;code&gt;__name__&lt;/code&gt; attribute on the function when
generating it. This has the extra benefit of not needing to return a name for
the assignment, we can just use the same:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; make_method&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__name__ &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;test_alphabetical_&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; add_methods&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    Take a TestCase and add a test method for each input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; decorator&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            test_input &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; make_method&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;            setattr&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__name__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; klass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; decorator&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now with the function named accurately, we can specify a single test method to run:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;nosetests&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; example/test_alphabetical.py:IsAlphabetizedTestCase.test_alphabetical_iOS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_alphabetical_iOS&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 8,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;), u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{} was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; iOS&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; considered&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Ran&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 0.001s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAILED&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (failures=1)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;generalize-the-test-method-factory&quot;&gt;Generalize the Test Method Factory&lt;/h2&gt;
&lt;p&gt;So our class decorator works well for our single test method generator. But
what if we have multiple test methods we want to generate? Remember our
initial example also included expected False results? Let’s make it so we can
pass in the test method:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;alphabetical &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; is_alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; unittest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; assert_is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;    self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; assert_is_not_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;    self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertFalse&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; make_method&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;        func&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__name__ &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;test_&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{func}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{input}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__name__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; generate&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    Take a TestCase and add a test method for each input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; decorator&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; inputs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            test_input &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; make_method&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;            setattr&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;klass&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__name__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; test_input&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; klass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; decorator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;@generate&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;assert_is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;a&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;aaaaa&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;ab&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;abcd&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;A-Cert&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;iOS&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;@generate&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;assert_is_not_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;ba&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;aba&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;bob&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; IsAlphabetizedTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;unittest&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can run all&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_assert_is_alphabetized_A-Cert&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 17,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    func(self,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 7,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; assert_is_alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;), u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{} was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; A-Cert&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; considered&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;======================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAIL:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_assert_is_alphabetized_iOS&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (example.test_alphabetical.IsAlphabetizedTestCase)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Traceback&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (most &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;recent&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 17,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; test_input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    func(self,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;  File&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;example/test_alphabetical.py&quot;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; line&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 7,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; assert_is_alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    self.assertTrue(is_alphabetized(input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;), u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{} was not considered alphabetized&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;AssertionError:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; iOS&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; was&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; considered&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; alphabetized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Ran&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; tests&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 0.001s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;FAILED&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; (failures=2)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Now with these handy generated test methods, we can make the necessary fixes
to our &lt;code&gt;is_alphabetical&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; re&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; is_alphabetized&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    value &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;lower&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    value &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; re&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;sub&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;a-z&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;sorted&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Ran&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; tests&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; 0.001s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ah, so satisfying….&lt;/p&gt;
</content:encoded><category>Python</category></item><item><title>Django Model Descriptors</title><link>https://blog.kevinastone.com/django-descriptors/</link><guid isPermaLink="true">https://blog.kevinastone.com/django-descriptors/</guid><description>How we can use Python Descriptors to create enhanced Django model fields that better encapsulate their functionality. This will help keep our code more modular and reusable and well as cleaner and more maintainable.</description><pubDate>Wed, 26 Nov 2014 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;How can we enhance our Django model fields to act beyond simple database types
to encapsulate their associated business logic? Leveraging Python’s
descriptor protocol, we provide additional processing on retrieval and update
to allow more re-usable fields.&lt;/p&gt;
&lt;h2 id=&quot;python-descriptors&quot;&gt;Python Descriptors&lt;/h2&gt;
&lt;p&gt;What are &lt;a href=&quot;https://docs.python.org/2/howto/descriptor.html&quot;&gt;Python Descriptors&lt;/a&gt;? They’re one of the least
understood aspects of the language, but I think of them like class-based
&lt;a href=&quot;https://docs.python.org/2/library/functions.html#property&quot;&gt;properties&lt;/a&gt;. Just as you can provide a &lt;code&gt;__call__&lt;/code&gt; method on a
class so that it acts like a function, you can supply a &lt;code&gt;__get__&lt;/code&gt; (or
&lt;code&gt;__set__&lt;/code&gt; or &lt;code&gt;__delete__&lt;/code&gt;) and the class will similarly act more like a
property.&lt;/p&gt;
&lt;p&gt;This allows us to capture common property definitions in a re-usable construct
that we can apply as generic functionality to our models. If you’re read the
post on &lt;a href=&quot;/django-model-behaviors&quot;&gt;Django Model Behaviors&lt;/a&gt;, you understand how
to package common field definitions into a shared model behavior that you can
combine to assemble your models from more atomic functionality. Leveraging
descriptors, you can perform a similar deconstruction but this time by
creating re-usable fields with enhanced logic without being tied to a given
model definition.&lt;/p&gt;
&lt;h4 id=&quot;sample-code&quot;&gt;Sample Code&lt;/h4&gt;
&lt;p&gt;All the sample code for this project is available on a &lt;a href=&quot;https://github.com/kevinastone/django-descriptors&quot;&gt;GitHub
repository&lt;/a&gt;. Feel free to use it as the basis for your own
experimentation.&lt;/p&gt;
&lt;h2 id=&quot;enhance-your-models-with-properties&quot;&gt;Enhance your Models with Properties&lt;/h2&gt;
&lt;p&gt;A common use of properties is to perform calculations or manipulations on a
given field and provide that as additional information.&lt;/p&gt;
&lt;p&gt;For example, let’s start with this Bookmark model that features a &lt;code&gt;url&lt;/code&gt; field:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;db &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    url &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;URLField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It’s very simple and straightforward. Now let’s say we want to extract some
additional information about the stored url, say it’s domain/hostname so we
can show a favicon on our listing. Since the url already contains the
hostname, we can add a method or a property to our model which retrieves the
url, and then parses out the hostname.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; urlparse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;db &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    url &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;URLField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;property&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; hostname&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; urlparse&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;urlparse&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;hostname&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, when we’re accessing our bookmark model instances, we can simple refer to
&lt;code&gt;obj.hostname&lt;/code&gt;. This pattern works great for many common derived or computed
values. No reason to store redundant information when we can calculate it on
the fly from other model attributes.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; boomark &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; Bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;http://example.com/abcd&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; # Let&apos;s access our hostname property&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;hostname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;example.com`&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But as we add more and more computed values, these properties tend to overload
our models, cluttering our codebase. Plus, we are frequently adding the same
properties again and again, repeating the same functionality for each field.
What we’d like is a way to associate a given property with the model field
that derives it.&lt;/p&gt;
&lt;h2 id=&quot;descriptors-as-reusable-properties&quot;&gt;Descriptors as Reusable Properties&lt;/h2&gt;
&lt;p&gt;Rather than define a bunch of properties, what we’d rather do is something
like: &lt;code&gt;obj.url.hostname&lt;/code&gt;, and encapsulate all the derived logic on the url
field. Yet when you access a model instance’s field, it’s simply returned
just like the original python type (str/unicode in the case of a &lt;em&gt;URLField&lt;/em&gt;).
These additional properties would be transparent to the rest of the code.&lt;/p&gt;
&lt;p&gt;So, we need to intercept the value returned by accessing the field on the
instance and &lt;strong&gt;enhance&lt;/strong&gt; it with our custom properties. This is where the
descriptor protocol comes into play. It gives us a chance to intercept and
substitute the value returned when an attribute is accessed on a given class
instance.&lt;/p&gt;
&lt;p&gt;To make this work, we actually need two components. First, we need our
descriptor which performs the intercept and substitution. Second, we need a
proxy model that acts like the original datatype, but is augmented with the
additional custom properties.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; URLFieldProxy&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;unicode&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;property&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; hostname&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; urlparse&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;urlparse&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;hostname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; ProxyFieldDescriptor&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; field_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; proxy_class&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;field_name &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; field_name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;proxy_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; proxy_class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt; __get__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; owner&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;        # grab the original value before we proxy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        value &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__dict__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;field_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; value &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;            # We can&apos;t proxy a None through a unicode sub-class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;proxy_class&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt; __set__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__dict__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;field_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our &lt;em&gt;Proxy&lt;/em&gt; class, &lt;code&gt;URLFieldProxy&lt;/code&gt;, sub-classes the base type that the field
would return (&lt;code&gt;unicode&lt;/code&gt; in this case). That ensures we have the same base
datatype. And then it adds a property for calculating the derived value(s).&lt;/p&gt;
&lt;p&gt;Our &lt;em&gt;Descriptor&lt;/em&gt; class, &lt;code&gt;ProxyFieldDescriptor&lt;/code&gt;, is a regular python object,
with the magic methods &lt;code&gt;__get__&lt;/code&gt; and &lt;code&gt;__set__&lt;/code&gt; that define a descriptor. It
features a constructor that takes a two arguments, the name of the field we
want to intercept, and the proxy class we want to substitute. The &lt;code&gt;__get__&lt;/code&gt;
implementation looks up the original value of the intercepted field using
&lt;code&gt;instance.__dict__&lt;/code&gt; and then instantiates our proxy class passing in that
original value. We special case &lt;code&gt;None&lt;/code&gt; since our proxy can’t handle that type
directly (&lt;em&gt;None&lt;/em&gt; values can’t have properties). &lt;code&gt;__set__&lt;/code&gt; simply stores the
value directly without modification.&lt;/p&gt;
&lt;p&gt;Before we talk through how to attach this descriptor to your django model
field, let’s put our implementation through some paces to show how a
descriptor operates.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; SomeObject&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;    # Let&apos;s add our descriptor on the `url` field substituting `URLFieldProxy`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    wormhole &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; ProxyFieldDescriptor&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;url&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; URLFieldProxy&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; url&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Remember that we defined our descriptor to look up it’s real value from the
passed in argument name. It then returns the &lt;em&gt;Proxy&lt;/em&gt; model that adds the
computed properties. Let’s see it in action:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; SomeObject&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;http://example.com/asdf&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; # Normal attribute access still works&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;http://example.com/asdf&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; # Does obj.url have a hostname property?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;hostname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;--------------------------------------------------------------------------&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;AttributeError&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;                            Traceback &lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;most recent call last&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; # What about accessing our descriptor field?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;wormhole&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;http://example.com/asdf&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; # Let&apos;s access the descriptor&apos;s property&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;wormhole&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;hostname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;example.com&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# As you can see, the descriptor returns our proxy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;wormhole&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__main__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;URLFieldProxy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# But the proxy still *acts* like our original url attribute&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;wormhole &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;customize-django-model-fields-with-descriptors&quot;&gt;Customize Django Model Fields with Descriptors&lt;/h3&gt;
&lt;p&gt;Now, we need to customize the Django &lt;code&gt;URLField&lt;/code&gt; to hook in to the descriptor
class. Thankfully, django provides an accessible mechanism under a method
named &lt;code&gt;contribute_to_class&lt;/code&gt; that allows you to customize how a field class is
attached to its model instance. The specifics of how this method works are
convoluted and related to the metaclass magic that django model definitions
perform.&lt;/p&gt;
&lt;p&gt;Since &lt;code&gt;contribute_to_class&lt;/code&gt; is used by django when building the model
instance, we simply attach our descriptor in the place of the field’s name on
our constructed model instance using &lt;code&gt;setattr()&lt;/code&gt; with &lt;code&gt;self.name&lt;/code&gt; representing
the name of the field (“url” in our case).&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; HostnamedURLField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;URLField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; contribute_to_class&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;        super&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;HostnamedURLField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;contribute_to_class&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;        # Add our descriptor to this field in place of of the normal attribute&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;        setattr&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; ProxyFieldDescriptor&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; URLFieldProxy&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, let’s update our model to use our customized url field. Notice our
model definition knows nothing about hostnames, it’s all encapsulated in our
customized &lt;em&gt;URLField&lt;/em&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    url &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; HostnamedURLField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the model all wired up with our descriptor wielding field, let’s
see if it works.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; bookmark &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; Bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;http://example.com/asdf&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;http://example.com/asdf&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;hostname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;example.com&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; # You can assign to the descriptor field too&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;http://somewhere.com/else&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; # And it still keeps all the same semantics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; bookmark&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;hostname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;somewhere.com&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There you go, we have a successfully migrated a computed property from the
model definition to the field definition. This helps keep common
functionality encapsulated, allowing greater re-use and cleaner separation of
concerns.&lt;/p&gt;
&lt;h2 id=&quot;descriptors-for-alternative-representations&quot;&gt;Descriptors for Alternative Representations&lt;/h2&gt;
&lt;p&gt;Besides just augmenting fields with computed properties, descriptors can also
be used to provide additional representations for your model data.&lt;/p&gt;
&lt;p&gt;One of my favorite database modeling patterns is to use timestamps for boolean
flags. If a timestamp is NULL, it’s off (or disabled). When a timestamp is
set, the flag is enabled. This gives you both a status (on/off) as well as a
history of when the flag was enabled.&lt;/p&gt;
&lt;p&gt;In most uses, we’re only concerned with the status, so a boolean data type is
more representative. But certain views would like to know this date
information (for say a management overview).&lt;/p&gt;
&lt;p&gt;Let’s create another simple model to walk through such a use case:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;db &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    content &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;TextField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    published_at &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;DateTimeField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With a &lt;code&gt;published_at&lt;/code&gt; field, we can show visitors posts that have been marked
ready for publication, as well as sort our blog posts based on their
publication date. This same pattern works great for any moderation task,
including comments, account setup, or content release.&lt;/p&gt;
&lt;p&gt;With our model, we can create a few posts and then publish them by setting our
timestamp field to a non-NULL value.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;published_at &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;published_at__isnull&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; # Now let&apos;s set our published flag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;published_at &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;save&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;published_at &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;published_at__isnull&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;creating-a-hybrid-booleantimestamp-descriptor&quot;&gt;Creating a Hybrid Boolean/Timestamp Descriptor&lt;/h3&gt;
&lt;p&gt;Using a datetime field gives us that extra information, but it makes working
with such fields less intuitive. You have to know it’s a datetime value
rather than a boolean. What if we could use descriptors to modify the
returned field to act like a boolean?&lt;/p&gt;
&lt;p&gt;Let’s create a descriptor class that proxies our datetime value, but acts like
a boolean.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; TimestampedBooleanDescriptor&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt; __get__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; owner&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__dict__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; is&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt; __set__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        value &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; value &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt;__get__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__dict__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;            else&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;__dict__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It handles two cases, the first is on &lt;code&gt;__get__&lt;/code&gt; where it checks its stored
timestamp against None. The other case is when &lt;code&gt;__set__&lt;/code&gt; is called, it checks
the input value (a boolean) and if it’s changed, either sets or clears the
internal datetime representation.&lt;/p&gt;
&lt;p&gt;Let’s build an example to use this descriptor.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; SomeObject&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;    # Let&apos;s add our descriptor on the `timestamp` field&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    boolean &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; TimestampedBooleanDescriptor&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;timestamp&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; timestamp&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timestamp &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timestamp&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And let’s put our sample object through some paces:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; SomeObject&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timestamp &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;boolean&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timestamp &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;boolean&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;boolean &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timestamp &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;boolean &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timestamp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;datetime&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;datetime&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;2014&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 12&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 6&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 21&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 34&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 12&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 872457&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; tzinfo&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;UTC&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Through the descriptor protocol, we were able to extend the functionality of
our fields to both support a datetime and a boolean interface.&lt;/p&gt;
&lt;p&gt;Now, let’s extend the built-in &lt;code&gt;DateTimeField&lt;/code&gt; to use our descriptor class to
provide both interfaces. Our field adds two capabilities: first, we want to
add a second property on the given model for the boolean access (it defaults
to &lt;code&gt;is_&amp;lt;field_name&amp;gt;&lt;/code&gt;). Second, we again use &lt;code&gt;contribute_to_class&lt;/code&gt; to add our
additional boolean property on the model class.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; TimestampedBooleanField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;DateTimeField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    A Boolean field that also captures the timestamp when the value was set.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    This field stores a timestamp in the database when set.  It can be accessed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    as a boolean using the property argument (when not provided, it defaults to&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    is_{field_name}).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;property_name &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;pop&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;property&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;        kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;        super&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;TimestampedBooleanField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89DCEB;font-style:italic&quot;&gt;__init__&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; contribute_to_class&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;        super&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;TimestampedBooleanField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;contribute_to_class&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;        # Use the defined boolean property name or pick a default&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        property_name &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;property_name &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;or&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;is_&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;{0}&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;        setattr&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; property_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; TimestampedBooleanDescriptor&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s now update our &lt;code&gt;BlogPost&lt;/code&gt; model to use this timestamp field.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    content &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;TextField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    published_at &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; TimestampedBooleanField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;property&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;is_published&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the model all wired up with our descriptor wielding field, let’s
see if it works.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;published_at &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;is_published&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;is_published &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;published_at&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;datetime&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;datetime&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;2014&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 12&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 6&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 21&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 47&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 17&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 191879&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; tzinfo&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;UTC&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Still works after reloading from the DB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;save&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post2 &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;pk&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;pk&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post2&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;is_published&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post2&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;published_at&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;datetime&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;datetime&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;2014&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 12&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 6&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 21&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 47&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 17&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 191879&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; tzinfo&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;UTC&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There you go, depending on how you want to access your model, you can choose
to use either the timestamp attribute or the boolean. All of this is
encapsulated in a descriptor class, keeping your model definition clean and
intuitive.&lt;/p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;There’s numerous opportunities to leverage descriptors to enhance your model
fields. Django itself uses them to handle File Upload fields since they’re
stored in the database as pathnames to the file, but provided as a file-like
proxies to your django code.&lt;/p&gt;
&lt;p&gt;There’s plenty of additional uses for descriptors. Please leave a comment (or
tweet, etc) if you have addditional ideas or examples where descriptors can
improve your model code.&lt;/p&gt;
</content:encoded><category>Django</category><category>Python</category></item><item><title>ElasticSearch Bliss with ElasticUtils</title><link>https://blog.kevinastone.com/elasticutils/</link><guid isPermaLink="true">https://blog.kevinastone.com/elasticutils/</guid><description>While Django Haystack remains the go to recommendation for adding simple search indexing to your Django sites, you can quickly outgrow the simplified “bag of text” data model that haystack attempts to unify the various supported indexing engines. Depending on your use case, you eventually need to customize the tokenizers, scoring, spell correct, autocomplete, etc. These more advanced use cases no longer work commonly across backends forcing you to leave the comfort of Haystack for native APIs for your chosen indexer. ElasticUtils is a newer project from the fine folks at Mozilla that exposes much of the rich capabilities of ElasticSearch in a more elegant, pythonic interface. Much like an ORM can simplify the process of generating SQL for your databsae queries, ElasticUtils provides a streamlined interface for generating search queries for the ElasticSearch Query DSL.</description><pubDate>Mon, 30 Dec 2013 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;While &lt;a href=&quot;http://haystacksearch.org&quot;&gt;Django Haystack&lt;/a&gt; remains the go to recommendation for adding simple search indexing to your Django sites, you can quickly outgrow the simplified “bag of text” data model that haystack attempts to unify the various supported indexing engines. Depending on your use case, you eventually need to customize the tokenizers, scoring, spell correct, autocomplete, etc. These more advanced use cases no longer work commonly across backends forcing you to leave the comfort of Haystack for native APIs for your chosen indexer. &lt;a href=&quot;https://github.com/mozilla/elasticutils&quot;&gt;ElasticUtils&lt;/a&gt; is a newer project from the fine folks at &lt;a href=&quot;http://www.mozilla.org/&quot;&gt;Mozilla&lt;/a&gt; that exposes much of the rich capabilities of &lt;a href=&quot;http://www.elasticsearch.org&quot;&gt;ElasticSearch&lt;/a&gt; in a more elegant, pythonic interface. Much like an &lt;a href=&quot;http://en.wikipedia.org/wiki/Object-relational_mapping&quot;&gt;ORM&lt;/a&gt; can simplify the process of generating SQL for your databsae queries, ElasticUtils provides a streamlined interface for generating search queries for the &lt;a href=&quot;http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html&quot;&gt;ElasticSearch Query DSL&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;why-elasticsearch&quot;&gt;Why ElasticSearch&lt;/h2&gt;
&lt;p&gt;Why &lt;a href=&quot;http://www.elasticsearch.org&quot;&gt;ElasticSearch&lt;/a&gt; could really be its own blog post. It’s an immensely powerful search indexing system built on top of the rock solid &lt;a href=&quot;http://lucene.apache.org&quot;&gt;Lucene&lt;/a&gt; library. It features a considerable number of built in query and filter types that allow great range in the type of search conditions allowed. In addition, it has many customizable tokenizers, analyzers and filters to transform your documents into readily identified search results. It’s most frequently compared to &lt;a href=&quot;http://lucene.apache.org/solr/&quot;&gt;SOLR&lt;/a&gt; (which is also Lucene based), which provides similar features. Comparatively, the ReST based API interface of ElasticSearch shifts much of the data definition and configuration to the client, eliminating the burdensome and more rigid system administration challenges of SOLR. Finally, ElasticSearch (as the name gives away), was built with clustering in mind from the start, providing a pathway to sharding your dataset relatively seamlessly for scalability as your system grows.&lt;/p&gt;
&lt;p&gt;Having said that, ElasticSearch is a powerful, but intricate search system. Don’t expect to be able to bolt it on to a project for a simple full-text search, there’s likely easier solutions on the market. There’s a lot of knobs and parameters, and you’re going to need to make an investment to fully understand the ElasticSearch architecture. ElasticUtils doesn’t really attempt to abstract away those details in the way that &lt;a href=&quot;http://haystacksearch.org&quot;&gt;Django Haystack&lt;/a&gt; may. It does have rather sensible defaults though, that make getting started more of a weekend project than a month long sabbatical.&lt;/p&gt;
&lt;p&gt;Ironically, ElasticSearch’s flexibility has opened it up to non-traditional indexing use cases such as &lt;a href=&quot;http://logstash.net&quot;&gt;log management&lt;/a&gt;, analytics and data mining.&lt;/p&gt;
&lt;h2 id=&quot;elasticutils-in-action&quot;&gt;ElasticUtils in Action&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://www.elasticsearch.org&quot;&gt;ElasticSearch&lt;/a&gt; allows creating rich, complex search queries using a &lt;a href=&quot;http://en.wikipedia.org/wiki/Representational_state_transfer&quot; title=&quot;Representational State Transfer&quot;&gt;ReSTful API&lt;/a&gt;. Beyond the basic, find documents that match the given terms, you can filter your searches by categories or other criteria (usually called faceting, such as those tag clouds or drill-downs). It features considerable function to develop powerful and specialized search applications.&lt;/p&gt;
&lt;p&gt;The challenge with many rich, expressive query languages, is how to generate these expressions reliability without a significant maintenance cost to the development team. Enter &lt;a href=&quot;https://github.com/mozilla/elasticutils&quot;&gt;ElasticUtils&lt;/a&gt;, which provides access to most of ElasticSearch’s features, but using a chainable queryset-like expression API for generating search parameters.&lt;/p&gt;
&lt;p&gt;As a case, here’s an actual (relatively simply) query generated from our search system looking for products related to “nike air max” (a line of shoes):&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;term&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;      &quot;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;is_visible&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;query_string&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;      &quot;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;default_field&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;_all&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;      &quot;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;nike air max&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This query, takes our user supplied input: “nike air max”, and generates a &lt;code&gt;query_string&lt;/code&gt; type query against the &lt;code&gt;_all&lt;/code&gt; field (which is a combination of all the indexed fields), and filters the results to ones that are marked public (&lt;code&gt;is_visible = true&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;As you can see, ElasticSearch queries are a highly nested JSON structure. Having to manage all these nodes and keep them in order can be complex and error prone.&lt;/p&gt;
&lt;p&gt;Contrast the same query using ElasticUtils:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; elasticutils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; S&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;query &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; S&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;is_visible&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;_all__query_string&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;nike air max&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice how we can construct the same nested structure, in a chainable fashion that allows us to augment or enhance our existing queries? What if we wanted to restrict our query just to products in our &lt;code&gt;shoes&lt;/code&gt; category?&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;query &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;category&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&quot;shoes&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;django-integration&quot;&gt;Django Integration&lt;/h2&gt;
&lt;p&gt;While &lt;a href=&quot;https://github.com/mozilla/elasticutils&quot;&gt;ElasticUtils&lt;/a&gt; is a general python library, it comes with some django integration out of the box to simplify integration. First, if you use the django-ized versions of the API constructs (&lt;code&gt;S&lt;/code&gt;, &lt;code&gt;F&lt;/code&gt;, etc), they will leverage the django settings system for configuration like ElasticSearch server URLs.&lt;/p&gt;
&lt;p&gt;Second, ElasticUtils has pre-defined &lt;a href=&quot;http://www.celeryproject.org&quot;&gt;Celery&lt;/a&gt; tasks for indexing and un-indexing your django models. This Makes it really straightforward to hook up a &lt;code&gt;post_save&lt;/code&gt; signal handler to your models to have them re-indexed when updated in real-time (vs the traditional overnight batch re-index).&lt;/p&gt;
&lt;p&gt;The one missing out-of-the-box component for Django is a management command for indexing your data. There’s an incomplete &lt;a href=&quot;https://github.com/mozilla/elasticutils/pull/168&quot;&gt;pull-request&lt;/a&gt; that’s unfortunately been sidelined for now. Hopefully the team behind ElasticUtils can come together and finish it off. In the meantime, you can easily roll your own in less than 100 lines of code.&lt;/p&gt;
&lt;h2 id=&quot;indexing-your-data&quot;&gt;Indexing your Data&lt;/h2&gt;
&lt;h3 id=&quot;assembling-your-mappings&quot;&gt;Assembling your Mappings&lt;/h3&gt;
&lt;p&gt;To define your indexes in &lt;a href=&quot;https://github.com/mozilla/elasticutils&quot;&gt;ElasticUtils&lt;/a&gt;, you define a &lt;code&gt;MappingType&lt;/code&gt; class much like in the Django ORM, you’d define a &lt;code&gt;Model&lt;/code&gt;. This allows you to provide the necessary parameters to ElasticSearch to define the properties of the index and provide helper methods to perform the indexing and construct the searches.&lt;/p&gt;
&lt;p&gt;The amount of boilerplate code required depends on whether you’re using the base &lt;code&gt;MappingType&lt;/code&gt; or the django-ized version. The django version leverages the Django ORM for retrieval of documents to index as an example.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; elasticutils&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;contrib&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;django &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Indexable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; MappingType&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; example&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;product&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;models &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Product&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;MappingType&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Indexable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;classmethod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_index&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;products&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;classmethod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_mapping_type_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;product&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;classmethod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Product&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;classmethod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_mapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;            # TBD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;classmethod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; extract_document&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; obj_id&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;            # TBD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There’s a couple methods with TBDs (&lt;code&gt;extract_document()&lt;/code&gt;, &lt;code&gt;get_mapping()&lt;/code&gt;) in there that we’ll revisit individually, but you can see much of this is scaffolding not unlike the &lt;code&gt;Meta&lt;/code&gt; class on Django models (but using classmethods rather than a nested inner class). These configuration methods for the most part just convert their output to JSON and are delivered to ElasticSearch un-modified.&lt;/p&gt;
&lt;h3 id=&quot;defining-your-data-mapping&quot;&gt;Defining your Data Mapping&lt;/h3&gt;
&lt;p&gt;The mapping configuration ultimately defines how ElasticSearch stores and indexes your data. It needs to know what fields are available in a given index, and parameters on how to consume and process those fields. If you’ve ever used &lt;a href=&quot;http://lucene.apache.org/solr/&quot;&gt;Apache SOLR&lt;/a&gt;, you’ll be relieved how much easier this configuration is compared to the XML-hell that you’ve surely experienced.&lt;/p&gt;
&lt;p&gt;For our example Product index, let’s create some common fields that might be associated with a product listing. This isn’t designed to be a full tutorial on elasticsearch, so we’ll have to skip over some of the more complex configuration options around analyzers, tokenizers and other indexing controls.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;classmethod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_mapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;        &apos;properties&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;type&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;string&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;            },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;description&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;type&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;string&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;analyzer&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;snowball&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;            },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;sku&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;type&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;string&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;index&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;not_analyzed&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;            },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;price&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;type&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;integer&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;index&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;not_analyzed&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;            },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;category&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;type&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;string&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;index&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;not_analyzed&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;            },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Most of our fields are designated as type &lt;code&gt;string&lt;/code&gt;, while our &lt;em&gt;price&lt;/em&gt; field is an integer (it will be number of cents, so &lt;code&gt;price_in_dollars * 100&lt;/code&gt;). You’ll notice that many of the fields have an attribute &lt;code&gt;analyzer: not_analyzed&lt;/code&gt;. This is an instruction to ElasticSearch not to attempt to parse the field and just leave it as is. Normally, ElasticSearch would split the field into terms based on word boundaries (see the &lt;a href=&quot;http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html&quot;&gt;standard analyzer&lt;/a&gt; which is the default). But for many of the fields, we want to it to be indexed and stored exactly as we delivered (such as for &lt;em&gt;sku&lt;/em&gt; which should be treated as a single term). For &lt;em&gt;description&lt;/em&gt;, we’ve used the &lt;a href=&quot;http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-snowball-analyzer.html&quot;&gt;snowball analyzer&lt;/a&gt; which provides additional filtering around stop words and stemming. You can also create your own analyzers for more custom indexing of a given field.&lt;/p&gt;
&lt;p&gt;This is just the tip of the iceberg. There’s a multitude of configuration parameters for each field in your mapping.&lt;/p&gt;
&lt;h3 id=&quot;extracting-fields-during-indexing&quot;&gt;Extracting Fields during Indexing&lt;/h3&gt;
&lt;p&gt;After defining the index mapping fields, let’s implement the other un-implemented method, &lt;code&gt;extract_document()&lt;/code&gt;, that captures and prepares your data into the relevant fields. This provides you the control in extracting and transforming your data prior to ingestion by elasticsearch.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;classmethod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; extract_document&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; obj_id&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            obj &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; cls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get_model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;pk&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;obj_id&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;id&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;pk&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;description&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;description&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;sku&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;sku&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;price&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;price &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;  # Convert to cents&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &apos;category&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;category&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;lower&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;   # assuming `category` is a foreign key&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we’ve extracted several attributes on our model as well the product category from a related model.&lt;/p&gt;
&lt;h3 id=&quot;create-indexes&quot;&gt;Create Indexes&lt;/h3&gt;
&lt;p&gt;Finally, With all your mappings configured, you can now create or update your indexes using elasticsearch. We simply generate a settings configuration based on the elasticsearch options and elasticutils does the work for us.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;es &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get_es&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;settings &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Add our mapping configuration to the index settings&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;settings&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get_mapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# TBD: Add index settings here such as custom analyzers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;es&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;create_index&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get_index&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; settings&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;settings&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, let’s leverage the built-in celery tasks to perform the index on our model data. To index a model, you simply provide its primary key to the &lt;code&gt;index_objects&lt;/code&gt; task, which as we’ve defined above in &lt;code&gt;extract_document()&lt;/code&gt;, will pull it from the database and extract the fields for indexing.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; elasticutils&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;contrib&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;django &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; tasks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;model_objs &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get_model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;model_ids &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;model_objs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;values_list&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;pk&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; flat&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;tasks&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;index_objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; model_ids&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;searching-your-index&quot;&gt;Searching your Index&lt;/h2&gt;
&lt;p&gt;Now that we’ve indexed our data, let’s walk through a couple example searches that can demonstration the richness of the ElasticSearch and ElasticUtils query expression API.&lt;/p&gt;
&lt;p&gt;Couple of notes on the ElasticUtils &lt;code&gt;S&lt;/code&gt; class that handles your search queries. They mimic many of the attributes of Django’s querysets.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;They’re chainable, so you can build up your search parameters through successive calls to add filters, queries and other parameters. Likewise, you can build-up common base search parameters and re-use it for a number of sub-queries that share those common parameters.&lt;/li&gt;
&lt;li&gt;They’re lazy, so search is only performed when you try to act on the data such as iterating, or calling specific methods like &lt;code&gt;count()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;search-for-matching-terms&quot;&gt;Search for Matching Terms&lt;/h3&gt;
&lt;p&gt;Let’s start with the obvious use case, we want to find all products who’s name contains the key search terms. With &lt;a href=&quot;http://lucene.apache.org&quot;&gt;Lucene&lt;/a&gt; based indexes, you usually can choose between a rich user supplied query language like a google search or constructing your search parameters more programatically (or a combination of both). If you’re looking to give your users a free-form input (again, like Google) to specify their search parameters with operators and other modifiers (for example: &lt;code&gt;nike AND &quot;air max&quot; or category:shoes&lt;/code&gt;), you’re like going to want to use ElasticSearch’s &lt;a href=&quot;http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html&quot;&gt;query string query&lt;/a&gt;. On the other hand, if your interface is more data oriented (say a bunch of checkboxes and sliders for controlling the search parameters), you’ll want more exact search controls, you’ll want to leverage ElasticSearch’s &lt;a href=&quot;http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-queries.html&quot;&gt;many other queries&lt;/a&gt; including &lt;code&gt;match&lt;/code&gt;, &lt;code&gt;range&lt;/code&gt; and &lt;code&gt;bool&lt;/code&gt; queries.&lt;/p&gt;
&lt;p&gt;ElasticUtils takes a cue from django’s querysets and allows you to specify the type of query using a double underscore qualifier. So you can search a given field using a type of query like: &lt;code&gt;[field_name]__[query_type]&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;s &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;search&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;results &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;name__query_string&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;nike &quot;air max&quot; or category:shoes&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; result &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; results&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;    # process the results&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If instead of a free-form query string, we could instead build up a search programmatically using another query type. ElasticSearch provides a &lt;a href=&quot;http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html&quot;&gt;prefix query&lt;/a&gt; that can provide a good way to autocomplete search terms that begin with the inputted search phrase. They’re are more efficient methods using ngrams, but this will suffice for our example.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;s &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;search&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Returns all results that start with &apos;nik&apos; in the `shoes` category&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;results &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;name__prefix&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;nik&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;category&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;shoes&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; result &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; results&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;    # process the results&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;faceted-search-results&quot;&gt;Faceted Search Results&lt;/h3&gt;
&lt;p&gt;One of the powerful features of ElasticSearch is the ability to quickly generate facets based on your indexed fields. If you’re unfamiliar with facets, think of a tag cloud, or a list of categories (such as brands on an e-commerce site) that allow you to drill-down through a list of results to find your intended document.&lt;/p&gt;
&lt;p&gt;In our example mapping, we have a &lt;em&gt;category&lt;/em&gt; field that would be a nice facet to help our users select which category they’re looking for within our search results. Let’s first search for “nike” and then display a list of categories that have matches for that product name.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;s &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;search&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Add facet results for `category`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;query &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;nike&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;facet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;category&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;facets &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;facet_counts&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Let&apos;s iterate the category facets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; facet &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; facets&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;category&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    category &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; facet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;term&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    count &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; facet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;counts&quot;&gt;Counts&lt;/h3&gt;
&lt;p&gt;Sometimes, you don’t necessarily need to retrieve the search results, but just want to display the number of results. Like the Django ORM, you can call &lt;code&gt;.count()&lt;/code&gt; to retrieve the number of results for a given query.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;s &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ProductMapping&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;search&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Returns all results that start with &apos;nik&apos; in the `shoes` category&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;num_results &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;name__prefix&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;nik&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;category&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;shoes&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Again, this is just a sampler. There are numerous ways to search in elasticsearch and these are but a few simple examples. As your queries grow in complexity, the chain-ability of ElasticUtils search API keeps everything manageable.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;There you have it, a method to build complex search systems using the powerful &lt;a href=&quot;http://www.elasticsearch.org&quot;&gt;ElasticSearch&lt;/a&gt; systsem in a very pythonic manner. If you’re familiar with the common Django ORM patterns, it’s even better.&lt;/p&gt;
&lt;p&gt;Hopefully this gives you an overview of the &lt;a href=&quot;https://github.com/mozilla/elasticutils&quot;&gt;ElasticUtils&lt;/a&gt; python library as a powerful interface to the ElasticSearch indexing and retrieval system. Much like the Django ORM makes working with databases easier compared with raw SQL, ElasticUtils makes performing complex search queries easier than building your own ElasticSearch ReST calls.&lt;/p&gt;
&lt;p&gt;This was really just an introduction, we only showed simple field extraction and text searching. Much of the power of elasticsearch is realized when you start implementing more complex patterns like faceting, range queries, autocomplete, spell correction and suggestions. If there’s interest (leave me a comment), I’ll work on follow-up posts on how to construct those patterns on top of ElasticUtils.&lt;/p&gt;
&lt;h3 id=&quot;caveats&quot;&gt;Caveats&lt;/h3&gt;
&lt;p&gt;I wrote this guide based on ElasticUtils v0.80. The development trunk recently ported from the underlying &lt;a href=&quot;https://github.com/rhec/pyelasticsearch&quot;&gt;pyelasticsearch&lt;/a&gt; library, to the officially blessed, &lt;a href=&quot;https://github.com/elasticsearch/elasticsearch-py&quot;&gt;elasticsearch-py&lt;/a&gt; python bindings. While ElasticUtils for the most part masks the need to understand the underlying library, esoteric or management commands may require direct library access.&lt;/p&gt;
</content:encoded><category>Django</category><category>Python</category><category>ElasticSearch</category></item><item><title>Getting Started with Django Rest Framework and AngularJS</title><link>https://blog.kevinastone.com/django-rest-framework-and-angular-js/</link><guid isPermaLink="true">https://blog.kevinastone.com/django-rest-framework-and-angular-js/</guid><description>A ReSTful API is becoming a standard component of any modern web application. The Django Rest Framework is powerful framework for developing ReST endpoints for your Django based project. AngularJS is modern javascript framework for creating complex web applications within the browser. It focuses on strong separation of concerns (MVC) and dependency injection to encourage creating maintainable (and testable) modules that can be integrated to develop rich client side functionality.</description><pubDate>Wed, 02 Oct 2013 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A &lt;a href=&quot;http://en.wikipedia.org/wiki/Representational_state_transfer&quot; title=&quot;Representational State Transfer&quot;&gt;ReSTful API&lt;/a&gt; is becoming a standard component of any modern web application. The &lt;a href=&quot;http://django-rest-framework.org&quot; title=&quot;Django Rest Framework&quot;&gt;Django Rest Framework&lt;/a&gt; is powerful framework for developing ReST endpoints for your Django based project. &lt;a href=&quot;http://angularjs.org&quot; title=&quot;Angular JS&quot;&gt;AngularJS&lt;/a&gt; is modern javascript framework for creating complex web applications within the browser. It focuses on strong separation of concerns (MVC) and dependency injection to encourage creating maintainable (and testable) modules that can be integrated to develop rich client side functionality.&lt;/p&gt;
&lt;p&gt;In this blog post, I’ll walk through creating a sample project that exposes a ReST API consumed by AngularJS on the client to showcase how to combine the frontend and backend frameworks to simplify creating complex applications. I’ll make heavy use of code examples to demonstrate both the solution and the process and there’s a companion &lt;a href=&quot;https://github.com/kevinastone/django-api-rest-and-angular&quot;&gt;Github project&lt;/a&gt; with all the code.&lt;/p&gt;
&lt;h2 id=&quot;lets-build-a-sample-django-project&quot;&gt;Let’s Build a Sample Django Project&lt;/h2&gt;
&lt;p&gt;For a sample project, let’s create a simple photo-sharing app (not unlike a rudimentary Instagram) and build a feed view for a given user to scan through all the photos shared on the site.&lt;/p&gt;
&lt;p&gt;All the sample code for this project is available on a &lt;a href=&quot;https://github.com/kevinastone/django-api-rest-and-angular&quot;&gt;GitHub repository&lt;/a&gt;. To setup the sample project in your own environment, consult the &lt;a href=&quot;https://github.com/kevinastone/django-api-rest-and-angular/blob/master/Readme.markdown&quot;&gt;installation instructions&lt;/a&gt; included in the repository. This includes installing AngularJS (and other javascript assets) via &lt;a href=&quot;http://yeoman.io&quot;&gt;bower+grunt&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Finally, there’s some sample data in fixtures available to demonstrate the API. We have a few users (&lt;code&gt;[&apos;Bob&apos;, &apos;Sally&apos;, &apos;Joe&apos;, &apos;Rachel&apos;]&lt;/code&gt;), two posts (&lt;code&gt;[&apos;This is a great post&apos;, &apos;Another thing I wanted to share&apos;]&lt;/code&gt;) and some sample photos as well. The included Makefile builds the sample data for you.&lt;/p&gt;
&lt;p&gt;Couple notes about the sample code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I’ll skip over the details on configuring, building and running the sample application. There’s &lt;a href=&quot;https://github.com/kevinastone/django-api-rest-and-angular/blob/master/Readme.markdown&quot;&gt;instructions&lt;/a&gt; in the &lt;a href=&quot;https://github.com/kevinastone/django-api-rest-and-angular&quot;&gt;repository&lt;/a&gt; that covers many of these details. Please report any issues on GitHub and I’ll be sure to fix them.&lt;/li&gt;
&lt;li&gt;I’ve written the frontend code using &lt;a href=&quot;http://coffeescript.org&quot;&gt;Coffee-Script&lt;/a&gt; since I find it more legible and more efficient (and a bit more Pythonic). There’s a supplied Grunt task that collects all the coffee script files and concats them into a single &lt;code&gt;script.js&lt;/code&gt; file for inclusion.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;project-data-layer-the-models&quot;&gt;Project Data Layer (the Models)&lt;/h3&gt;
&lt;p&gt;Our model layer is straightforward to what you might find in an introductory tutorial for Django. You have three models of note: &lt;code&gt;User&lt;/code&gt;, &lt;code&gt;Post&lt;/code&gt;, and &lt;code&gt;Photo&lt;/code&gt;. A user can author many posts (as well as have many followers) and a post can showcase many photos (such as a collection or gallery) along with a title and optional description/body.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;db &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;contrib&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;auth&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;models &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; AbstractUser&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; User&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;AbstractUser&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    followers &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;ManyToManyField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;self&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; related_name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;followees&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; symmetrical&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    author &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;ForeignKey&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;User&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; related_name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    title &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;CharField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;max_length&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;255&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    body &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;TextField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;blank&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Photo&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    post &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;ForeignKey&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; related_name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;photos&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    image &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;ImageField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;upload_to&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&quot;%Y/%m/&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;django-rest-framework-based-api&quot;&gt;Django Rest Framework based API&lt;/h3&gt;
&lt;p&gt;The &lt;a href=&quot;http://django-rest-framework.org&quot; title=&quot;Django Rest Framework&quot;&gt;Django Rest Framework (DRF)&lt;/a&gt; provides a clean architecture to develop both simple, turn-key API endpoints as well as more complex ReST constructs. The key is a clean separation with &lt;code&gt;Serializer&lt;/code&gt; which describes the mapping between a model and the generalized wire representation (be it JSON, XML or whatever) and separate set of generic Class-Based-Views that can be extended to meet the needs of the specific API endpoint. You also define your own URL structure rather than rely on an auto-generated one. This is what separates DRF from other frameworks like &lt;a href=&quot;http://tastypieapi.org&quot;&gt;Tastypie&lt;/a&gt; and &lt;a href=&quot;https://bitbucket.org/jespern/django-piston/wiki/Home&quot;&gt;Piston&lt;/a&gt; that automate much of the conversion from django models to ReST endpoints, but come at the cost of flexibility in adapting to different use cases (especially around permissions and nested resources).&lt;/p&gt;
&lt;h3 id=&quot;model-serializers&quot;&gt;Model Serializers&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;Serializers&lt;/code&gt; in DRF focus on the responsibility to convert django model instances into their representation in the API. This gives us the opportunity to convert any data types, or provide supplemental information in a given model. For example, for the user, we only included some of the fields, stripping private attributes such as &lt;code&gt;password&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt;. For the photo, we converted the &lt;code&gt;ImageField&lt;/code&gt; to return the url of the image (rather than the media path location).&lt;/p&gt;
&lt;p&gt;For the &lt;code&gt;PostSerializer&lt;/code&gt;, we elected to embed the author directly in the Post (rather the the common case to provide a hyperlink). This makes that information readily accessible to our client rather than requiring extra API requests at the cost of duplicating users on each post. The alternative hyperlink is listed with a comment for comparison. The power of serializers is that you can extend them to create a derivative version, that uses hyperlinks instead of nested records (say, for the case of listing posts by a specific users’ feed).&lt;/p&gt;
&lt;p&gt;To assign the &lt;code&gt;author&lt;/code&gt; to the &lt;code&gt;PostSerializer&lt;/code&gt;, we’re gonna have that provided by the API View. So we’ll make it optional (&lt;code&gt;required=False&lt;/code&gt;) in our serializer and add it to the validation exclusion.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; rest_framework &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; serializers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;models &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; User&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Photo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; UserSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;serializers&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;ModelSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    posts &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; serializers&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;HyperlinkedIdentityField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; view_name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;userpost-list&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; lookup_field&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Meta&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; User&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        fields &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;id&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;username&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;first_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;last_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PostSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;serializers&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;ModelSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    author &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; UserSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    photos &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; serializers&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;HyperlinkedIdentityField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;photos&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; view_name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;postphoto-list&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;    # author = serializers.HyperlinkedRelatedField(view_name=&apos;user-detail&apos;, lookup_field=&apos;username&apos;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_validation_exclusions&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;        # Need to exclude `author` since we&apos;ll add that later based off the request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        exclusions &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; super&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;PostSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get_validation_exclusions&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; exclusions &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;author&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Meta&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PhotoSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;serializers&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;ModelSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    image &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; serializers&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;Field&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;image.url&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Meta&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Photo&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Okay, given our samples are fixtures are loaded, let’s play with these serializers. You’ll likely see &lt;code&gt;DeprecationWarning&lt;/code&gt; because we’re using &lt;code&gt;HyperlinkedIdentityField&lt;/code&gt; without providing a request object to construct the URL. In the actual views, this is provided, so you can safely ignore.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; example&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;api&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;models &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; User&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; user &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; User&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;username&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;bob&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; # Need to generate a fake request for our hyperlinked results&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;client &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; RequestFactory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; example&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;api&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;serializers &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; *&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; context &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; dict&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;RequestFactory&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;/&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; serializer &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; UserSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;user&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; context&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; serializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;id&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;username&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;bob&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;first_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;Bob&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;last_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;http://testserver/api/users/bob/posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; user&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()[&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; PostSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; context&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;author&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;id&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;username&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;bob&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;first_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;Bob&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;last_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;http://testserver/api/users/bob/posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;photos&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;http://testserver/api/posts/2/photos&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;id&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;title&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;Title #2&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;body&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;Another thing I wanted to share&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; serializer &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; PostSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;user&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; many&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; context&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; serializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[{&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;author&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;id&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;username&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;bob&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;first_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;Bob&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;last_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;http://testserver/api/users/bob/posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;photos&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;http://testserver/api/posts/2/photos&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;id&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;title&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;Title #2&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;body&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;Another thing I wanted to share&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;}]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;api-url-structure&quot;&gt;API URL Structure&lt;/h3&gt;
&lt;p&gt;For our API structure, we want to maintain a relatively flat structure to try to define canonical endpoints for given resources, but also provide some convenient nested listings for common filterings (such as posts for a given user and photos in a given post). Note that we use model primary keys as the identifier, but for users, we use their username since that’s also unique identifying (we’ll see this later in the views).&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;conf&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;urls &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; patterns&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; include&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;api &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; UserList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; UserDetail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;api &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostDetail&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; UserPostList&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;api &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PhotoList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PhotoDetail&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostPhotoList&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;user_urls &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; patterns&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^/&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;?P&amp;lt;username&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;0-9a-zA-Z_-&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;/posts$&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; UserPostList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;as_view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;userpost-list&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^/&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;?P&amp;lt;username&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;0-9a-zA-Z_-&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; UserDetail&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;as_view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;user-detail&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^$&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; UserList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;as_view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;user-list&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;post_urls &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; patterns&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^/&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;?P&amp;lt;pk&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;\d&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;/photos$&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostPhotoList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;as_view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;postphoto-list&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^/&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;?P&amp;lt;pk&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;\d&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostDetail&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;as_view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;post-detail&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^$&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;as_view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;post-list&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;photo_urls &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; patterns&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^/&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;?P&amp;lt;pk&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;\d&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PhotoDetail&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;as_view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;photo-detail&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^$&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PhotoList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;as_view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;photo-list&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;urlpatterns &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; patterns&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^users&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; include&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;user_urls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^posts&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; include&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;post_urls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;    url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1;font-style:italic&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;^photos&lt;/span&gt;&lt;span style=&quot;color:#F5C2E7&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; include&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;photo_urls&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;api-views&quot;&gt;API Views&lt;/h3&gt;
&lt;p&gt;Much of the power of Django Rest Framework is that the generic views make it easy to work with the common &lt;a href=&quot;http://en.wikipedia.org/wiki/Create,_read,_update_and_delete&quot;&gt;CRUD&lt;/a&gt; cases with little or no modifications. For the simplest views, you provide a &lt;code&gt;model&lt;/code&gt; and a &lt;code&gt;serializer_class&lt;/code&gt; and extend one of the built in generics (like &lt;code&gt;ListAPIView&lt;/code&gt; or &lt;code&gt;RetrieveAPIView&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;For our use case, we have a couple customizations. First, for users, we wanted to use &lt;code&gt;username&lt;/code&gt; as the lookup field rather than &lt;code&gt;pk&lt;/code&gt;. So we set &lt;code&gt;lookup_field&lt;/code&gt; on the view (by default it’s both the &lt;code&gt;url_kwarg&lt;/code&gt; and the field name on the model).&lt;/p&gt;
&lt;p&gt;We also wanted to create nested versions of the views for a given user’s posts or the photos within a post. You simply override &lt;code&gt;get_queryset&lt;/code&gt; on the view to customize the queryset to filter down the results based on the nested parameters (&lt;code&gt;username&lt;/code&gt; and &lt;code&gt;pk&lt;/code&gt; respectively).&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; rest_framework &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; permissions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;serializers &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; UserSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostSerializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PhotoSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;models &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; User&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Photo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; UserList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;ListCreateAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; User&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; User&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    serializer_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; UserSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    permission_classes &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        permissions&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;AllowAny&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; UserDetail&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;RetrieveAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; User&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; User&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    serializer_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; UserSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    lookup_field &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;username&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PostList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;ListCreateAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    serializer_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    permission_classes &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        permissions&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;AllowAny&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PostDetail&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;RetrieveUpdateDestroyAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    serializer_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    permission_classes &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        permissions&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;AllowAny&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; UserPostList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;ListAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    serializer_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_queryset&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; super&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;UserPostList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get_queryset&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; queryset&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;author__username&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PhotoList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;ListCreateAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Photo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Photo&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    serializer_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PhotoSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    permission_classes &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        permissions&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;AllowAny&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PhotoDetail&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;RetrieveUpdateDestroyAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Photo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Photo&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    serializer_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PhotoSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    permission_classes &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        permissions&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;AllowAny&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PostPhotoList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;ListAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Photo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Photo&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    serializer_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PhotoSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_queryset&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; super&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;PostPhotoList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get_queryset&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; queryset&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;post__pk&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;pk&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;interlude-built-in-api-browser&quot;&gt;Interlude: Built-in API Browser&lt;/h3&gt;
&lt;p&gt;One of the benefits of Django Rest Framework, is that it comes with a built-in API browser for testing out your API. Much like Django’s admin, this can help with initial development.&lt;/p&gt;
&lt;p&gt;Simple load one of your API endpoints in the browser, and through content-negotiation, DRF will present to you a handy client interface to interact with your API.&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;quot;alt&amp;quot;:&amp;quot;API Browser Example for Users&amp;quot;,&amp;quot;src&amp;quot;:&amp;quot;images/django-rest-framework-and-angular-js/browser_users.png&amp;quot;,&amp;quot;index&amp;quot;:0}&quot;&gt;
&lt;img __ASTRO_IMAGE_=&quot;{&amp;quot;alt&amp;quot;:&amp;quot;API Browser Example for User Posts&amp;quot;,&amp;quot;src&amp;quot;:&amp;quot;images/django-rest-framework-and-angular-js/browser_user_posts.png&amp;quot;,&amp;quot;index&amp;quot;:0}&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;add-permissions-and-ownership-to-api&quot;&gt;Add Permissions and Ownership to API&lt;/h3&gt;
&lt;p&gt;As previously written, our API Views allow anyone to create anything on our site. Some of the advantages of using Django Rest Framework, is that the generic views make it easy to control permissions in the view without impact the underlying model and serializers. To incorporate some control over who is allowed to edit our API resources, we can created some permission classes that provide the authorization controls to restrict. They should return a boolean for the given request. This gives us access to the full request including cookies, the authenticated user and more.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; rest_framework &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; permissions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; SafeMethodsOnlyPermission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;permissions&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;BasePermission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;Only can access non-destructive methods (like GET and HEAD)&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; has_permission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; request&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;has_object_permission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; has_object_permission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; request&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; request&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;method &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; permissions&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;SAFE_METHODS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PostAuthorCanEditPermission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;SafeMethodsOnlyPermission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;    &quot;&quot;&quot;Allow everyone to list or view, but only the other can modify existing instances&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; has_object_permission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; request&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;            # Either a list or a create, so no author&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            can_edit &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        else&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            can_edit &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; request&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;user &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;author&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; can_edit &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;or&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; super&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;PostAuthorCanEditPermission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;has_object_permission&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; view&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Besides simple authorization decisions, we also want to pre-fill values on save depending on who makes the requests. When someone creates a new &lt;code&gt;Post&lt;/code&gt;, we want to assign them as the author. Because of the duplication between &lt;code&gt;PostList&lt;/code&gt; and &lt;code&gt;PostDetail&lt;/code&gt;, we can create a mixin class that holds all the common configuration (you can also use [ViewSets][viewsets] to accomplish similar consolidation).&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PostMixin&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    queryset &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    serializer_class &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostSerializer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    permission_classes &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        PostAuthorCanEditPermission&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; perform_create&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; serializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;        &quot;&quot;&quot;Force author to the current user on save&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        serializer&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;save&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;user&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PostList&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;PostMixin&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;ListCreateAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PostDetail&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;PostMixin&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; generics&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;RetrieveUpdateDestroyAPIView&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;consume-the-api-using-angularjs&quot;&gt;Consume the API using AngularJS&lt;/h2&gt;
&lt;p&gt;With the advent of more interactive web applications, a ReSTful API can be leveraged by rich client interfaces to expose and interact with your application’s data model. AngularJS is a great companion because of its clean separation of controls. AngularJS’s modular architecture involves a little bit of setup. Your app is composed of modules which define services, directives and controllers that compartmentalize the functionality for cleaner separation.&lt;/p&gt;
&lt;p&gt;Part of the power of AngularJS, is that it provides &lt;a href=&quot;http://en.wikipedia.org/wiki/Reactive_programming&quot;&gt;reactive programming&lt;/a&gt; leveraging their javascript-like expression language. We can simply define a template that references variables, and our page will automatically refresh when changes are made to those variables.&lt;/p&gt;
&lt;p&gt;For the &lt;em&gt;hello world&lt;/em&gt; style intro, we’ll simply list the posts in our sample application. Here is a bare bones template in AngularJS. First, at the top level on the body tag, we specify which angular app we are using to run this page (&lt;code&gt;example.app.basic&lt;/code&gt;), which is where we’ll define the root module. Second, we need to specific the controller to manage our section (&lt;code&gt;AppController&lt;/code&gt;). The controller in AngularJS parlance is really more like the model+controller in the traditional MVC (with the injected &lt;code&gt;$scope&lt;/code&gt; containing the model state). Controllers define scopes that hold the actual model instances and they can be nested to narrow the scope of your system as your go down the DOM. Finally, we leverage an Angular directive (&lt;code&gt;ng-repeat&lt;/code&gt;) which is a control structure to iterate over our &lt;code&gt;posts&lt;/code&gt; model stored in the &lt;code&gt;$state&lt;/code&gt;. Within that iteration, we define a few tags and use the angular expression syntax (kinda like django template tags) to print out the username of the author, and the title and body of the post.&lt;/p&gt;
&lt;p&gt;*Note: Use verbatim tags to encapsulate the AngularJS expressions so that django doesn’t attempt to render them.&lt;/p&gt;
&lt;p&gt;*Note: I’ve enclosed some sections in Django &lt;code&gt;{% block %}&lt;/code&gt; tags so I can extend this template for each example.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;load&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; staticfiles %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;bootstrap/dist/css/bootstrap.css&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;body ng-app=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ng_app %}example.app.static{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endblock&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;div class=&quot;content&quot; ng-controller=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ng_controller %}AppController{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endblock&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; content %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;verbatim&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;div class=&quot;panel&quot; ng-repeat=&quot;post in posts&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;div class=&quot;panel-heading clearfix&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            &amp;lt;h3 class=&quot;panel-title&quot;&amp;gt;{{ post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;title }}&amp;lt;/h3&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            &amp;lt;author class=&quot;pull-right&quot;&amp;gt;{{ post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;username }}&amp;lt;/author&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;p class=&quot;well&quot;&amp;gt;{{ post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;body }}&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endverbatim&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endblock&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;script src=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;underscore/underscore.js&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;script src=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;angular/angular.js&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;script src=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;angular-resource/angular-resource.js&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;script src=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;js/script.js&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, let’s pair this template with a simple controller that will supply the list of posts. We’ll hard-code the posts for now and then pull them in with Ajax in the next step.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;coffeescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.static&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;controller&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;AppController&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$scope&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;$http&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; $http&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        author&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            username&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Joe&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        title&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Sample Post #1&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        body&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;This is the first sample post&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    ,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        author&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            username&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Karen&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        title&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Sample Post #2&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        body&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;This is another sample post&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you’ve got everything setup and run grunt to compile this down to javascript, you should see our &lt;a href=&quot;http://localhost:8000/static&quot;&gt;hard-coded template&lt;/a&gt; rendered as such:&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;quot;alt&amp;quot;:&amp;quot;Basic Post List - Hard Coded&amp;quot;,&amp;quot;src&amp;quot;:&amp;quot;images/django-rest-framework-and-angular-js/example_static.png&amp;quot;,&amp;quot;index&amp;quot;:0}&quot;&gt;&lt;/p&gt;
&lt;h4 id=&quot;using-xhr-to-pull-posts-from-the-api&quot;&gt;Using XHR to Pull Posts from the API&lt;/h4&gt;
&lt;p&gt;Now, let’s go ahead and update or controller to instead grab the list of posts from our API endpoint. The &lt;code&gt;$http&lt;/code&gt; service for AngularJS is similar to jQuery’s &lt;code&gt;$.ajax&lt;/code&gt; or other XHR implementations. Notice, with AngularJS, we simply updated our model (&lt;code&gt;$scope.posts&lt;/code&gt;) with the result of the ajax call, and our view is automatically updated in sync. No need to manipulate the DOM. This reactive style allows us to develop complex UIs with interdependencies between our data model and have the UI components respond appropriately without directly managing those connections, keeping our views and model layers loosely-coupled.&lt;/p&gt;
&lt;p&gt;*Note the use of the promise chaining (&lt;code&gt;.then()&lt;/code&gt;) on the &lt;code&gt;$http&lt;/code&gt; call. You could just as easily pass a callback. You can take advantage of promise chaining to develop more complex API workflows such as creating a new post and then saving 3 photos.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;coffeescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.basic&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;controller&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;AppController&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$scope&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;$http&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; $http&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $http&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;/api/posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; result&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;item&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; item&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With &lt;code&gt;$http&lt;/code&gt; pulling the list of posts from our API, our &lt;a href=&quot;http://localhost:8000/basic&quot;&gt;example page&lt;/a&gt; now shows a feed of posts from the server.&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;quot;alt&amp;quot;:&amp;quot;Basic Post List - XHR&amp;quot;,&amp;quot;src&amp;quot;:&amp;quot;images/django-rest-framework-and-angular-js/example_basic.png&amp;quot;,&amp;quot;index&amp;quot;:0}&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;leveraging-angular-resource-for-api&quot;&gt;Leveraging Angular-Resource for API&lt;/h3&gt;
&lt;p&gt;While &lt;code&gt;$http&lt;/code&gt; allows us to make the XHR calls to pull the API data into our app, it’s forcing us to hard code many details about our API, including constructing the URLs, http verbs and other aspects that could be encapsulated in a higher level construct. Enter &lt;a href=&quot;http://docs.angularjs.org/api/ngResource&quot;&gt;Angular-Resource&lt;/a&gt;, which provides a mechanism to define our API through Angular services that manage much of the lower level http requirements for us leaving simple ReSTful verbs to interact with the API.&lt;/p&gt;
&lt;p&gt;To work with Angular-Resource (or ngResource), you simply define the mapping between your API endpoints and the parameters in the URL pattern (much like Django &lt;code&gt;urlpatterns&lt;/code&gt;). Unfortunately, it’s not terribly easy to convert between Django and the ngResource definitions, so the DRY isn’t quite there.&lt;/p&gt;
&lt;p&gt;When you define your resources (using &lt;code&gt;$resource&lt;/code&gt;), you simple provide a url pattern, a list of default parameter mappings, and optionally some additional http methods. In our case, we want the resource for &lt;code&gt;User&lt;/code&gt; to pull the &lt;code&gt;:username&lt;/code&gt; parameter in the URL from the &lt;code&gt;username&lt;/code&gt; field on our resource instance. &lt;code&gt;Post&lt;/code&gt; and &lt;code&gt;Photo&lt;/code&gt; use the &lt;code&gt;id&lt;/code&gt; field on the instance accordingly as the primary key.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;coffeescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.api&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;ngResource&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;factory&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;User&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$resource&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$resource&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    $resource&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;/api/users/:username&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; username&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;@username&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;factory&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Post&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$resource&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$resource&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    $resource&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;/api/posts/:id&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;@id&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;factory&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Photo&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$resource&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$resource&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    $resource&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;/api/photos/:id&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;@id&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we’ve defined our api module, we can use it as a dependency in our controller module and inject those API resources as services that our controller can use to access the API. We add &lt;code&gt;example.api&lt;/code&gt; as a dependent module and list any API resources as dependencies in our controller definition. By default, your resources have many of the basic CRUD methods including &lt;code&gt;query()&lt;/code&gt; (for retrieving a collection), &lt;code&gt;get()&lt;/code&gt; (for individual items), &lt;code&gt;save()&lt;/code&gt;, &lt;code&gt;delete()&lt;/code&gt;, and so on.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;coffeescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.resource&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;example.api&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;controller&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;AppController&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$scope&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Post&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which results in the same list of Posts as above on the &lt;a href=&quot;http://localhost:8000/resource&quot;&gt;example page&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;quot;alt&amp;quot;:&amp;quot;Basic Post List - Resource&amp;quot;,&amp;quot;src&amp;quot;:&amp;quot;images/django-rest-framework-and-angular-js/example_resource.png&amp;quot;,&amp;quot;index&amp;quot;:0}&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;add-the-photos-to-our-post-feed&quot;&gt;Add the Photos to our Post Feed&lt;/h3&gt;
&lt;p&gt;We now have our feed of posts displaying using &lt;code&gt;ngResource&lt;/code&gt; API calls, but we’re simply making one call to retrieve the data. In real applications, your data is rarely stored at only a single resource endpoint and will require coordinated API calls to construct your applications model state. Let’s enhance our app to also retrieve the photos from each post and display them as well to explore this.&lt;/p&gt;
&lt;p&gt;First, let’s add two other resources for the nested API calls:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;coffeescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;factory&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;UserPost&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$resource&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$resource&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    $resource&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;/api/users/:username/posts/:id&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;factory&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;PostPhoto&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$resource&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$resource&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;    $resource&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;/api/posts/:post_id/photos/:id&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will provide two more services (&lt;code&gt;UserPost&lt;/code&gt; and &lt;code&gt;PostPhoto&lt;/code&gt;) that we can query to retrieve resources for a given user and post respectively. As nested resources, we want to wire up AngularJS to load them after loading the base resource (another option is to use angular’s &lt;code&gt;$watch&lt;/code&gt; mechanism to response to changes and trigger additional API calls). For this, we want to leverage Angular’s &lt;code&gt;$q&lt;/code&gt; service which provides a Promise/Deferred implementation allowing us to chain calls. Starting with ngResource-1.1+, each resource provides a $promise attribute which you can chain against. We are going to leverage that interface to make a follow on API call to retrieve photos for a given post.&lt;/p&gt;
&lt;p&gt;You have a couple of choices on how to handle the nesting of resources. For this case, we will simply create another container on our &lt;code&gt;$scope&lt;/code&gt; for the photos and use the post_id as the identifier. Angular’s expression and template language ignores missing keys, so we can simply iterate over &lt;code&gt;photos[post.id]&lt;/code&gt; to retrieve the photos in the template. Notice how we don’t have to signal to change the view/template. Angular’s &lt;code&gt;$digest&lt;/code&gt; process detects the updates (it’s also well integrated with &lt;code&gt;ngResoruce&lt;/code&gt; and &lt;code&gt;$q&lt;/code&gt;s).&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;coffeescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.photos&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;example.api&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;controller&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;AppController&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$scope&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Post&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;PostPhoto&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; PostPhoto&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;photos &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;$promise&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;results&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;        # Load the photos&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; results&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;photos&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PostPhoto&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;post_id&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And also update our template to iterate over this model to display photos in each post. Notice how AngularJS re-renders the view as the data is loaded from the API. In this case, we are iterating over the photos object referenced by the id of the post we were iterating in the parent &lt;code&gt;ng-repeat&lt;/code&gt;. We also used &lt;code&gt;ng-src&lt;/code&gt; instead of &lt;code&gt;src&lt;/code&gt; because it prevents browsers from attempting to load an image before the angular expression language has been evaluated (you’ll see 404s in your logs around can’t find: ‘/media/{{ photo.image }}’).&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;div class=&quot;panel&quot; ng-repeat=&quot;post in posts&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;div class=&quot;panel-heading clearfix&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;h3 class=&quot;pull-left panel-title&quot;&amp;gt;{{ post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;title }}&amp;lt;/h3&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;author class=&quot;pull-right&quot;&amp;gt;{{ post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;username }}&amp;lt;/author&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;p class=&quot;well&quot;&amp;gt;{{ post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;body }}&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;span class=&quot;photo&quot; ng-repeat=&quot;photo in photos[post.id]&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;img class=&quot;img-thumbnail&quot; ng-src=&quot;{{ photo&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;image }}&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And finally, our updated &lt;a href=&quot;http://localhost:8000/photos&quot;&gt;example page&lt;/a&gt; rendered with photos.&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;quot;alt&amp;quot;:&amp;quot;Basic Post List - Photos&amp;quot;,&amp;quot;src&amp;quot;:&amp;quot;images/django-rest-framework-and-angular-js/example_photos.png&amp;quot;,&amp;quot;index&amp;quot;:0}&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;interlude-angularjs--csrf-protection&quot;&gt;Interlude: AngularJS + CSRF Protection&lt;/h3&gt;
&lt;p&gt;Django Rest Framework extends Django’s &lt;a href=&quot;https://docs.djangoproject.com/en/dev/ref/contrib/csrf/&quot;&gt;Cross Site Request Forgery&lt;/a&gt; protection when using &lt;code&gt;SessionAuthentication&lt;/code&gt; (such as our case using the same browser session as the web application). This helps ensure malicious scripts can’t coax our users into making inadvertent calls to our API by forcing the scripts to return a server provided token on each call. AngularJS’s modular architecture and dependency injection configuration make this simple to configure our API calls to include the CSRF Token in a header (you can also use a cookie if you prefer).&lt;/p&gt;
&lt;p&gt;In our django template, simply add a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag to configure the &lt;code&gt;$http&lt;/code&gt; provider (AngularJS parlance for the implementation of the &lt;code&gt;$http&lt;/code&gt; dependency) and customize it to use the Django &lt;code&gt;{{ csrf_token }}&lt;/code&gt; template variable to set the CSRF Header on all API calls.&lt;/p&gt;
&lt;p&gt;*Note: Make sure this script is loaded &lt;strong&gt;after&lt;/strong&gt; your module definitions.&lt;/p&gt;
&lt;p&gt;*Note: You can pass the CSRF token between Angular and Django through cookies or other mechanisms. This explicit header mechanism just ensures the &lt;code&gt;csrf token&lt;/code&gt; is generated on every request.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;html&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;    // Add the CSRF Token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; app &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; angular&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;example.app&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt; // Not including a list of dependent modules (2nd parameter to `module`) &quot;re-opens&quot; the module for additional configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    app&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;config&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;([&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;        &apos;$httpProvider&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        function&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$httpProvider&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $httpProvider&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;defaults&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;headers&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;common[&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;X-CSRFToken&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;                &apos;{{ csrf_token|escapejs }}&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    ])&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;creatingmodifying-api-resource-using-angularjs&quot;&gt;Creating/Modifying API Resource using AngularJS&lt;/h2&gt;
&lt;p&gt;Let’s now build an editor in our feed view to publish a new post (such as a Facebook status update). While most Angular tutorials would just add the functionality to the existing controller on the page, we want to show how to keep your controllers lean and modular, so we’ll create a separate controller for this post editor and show how controllers leverage the nesting of the model scopes to extend functionality. This also gives a chance to extend our existing module &lt;code&gt;example.app.photos&lt;/code&gt; which provides our base &lt;code&gt;AppController&lt;/code&gt; for the feed.&lt;/p&gt;
&lt;p&gt;First, we need to extend our base template, and add the html template for the editor. We also add the CSRF token from the above instructions since we’ll now send unsafe methods to our API to save the new posts.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;extends&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;base.html&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %} {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ng_app %}example.app.editor{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endblock&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %} {%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; content %} {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;verbatim&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;div ng-controller=&quot;EditController&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;h5&amp;gt;Create a New Post&amp;lt;/h5&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;form class=&quot;form-inline&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;div class=&quot;form-group block-level&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            &amp;lt;input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                type=&quot;text&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                class=&quot;form-control&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                ng-model=&quot;newPost.title&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                placeholder=&quot;Title&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            /&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;div class=&quot;form-group&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            &amp;lt;input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                type=&quot;text&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                class=&quot;form-control&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                ng-model=&quot;newPost.body&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                placeholder=&quot;Body&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            /&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;div class=&quot;form-group&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            &amp;lt;button class=&quot;btn btn-default&quot; ng-click=&quot;save()&quot;&amp;gt;Add Post&amp;lt;/button&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;/form&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endverbatim&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %} {{ block&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;super }} {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endblock&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %} {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; js %} {{ block&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;super&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;}}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    // Add the CSRF Token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    var app = angular.module(&apos;example.app.editor&apos;); // Not including a list of dependent modules (2nd parameter to `module`) &quot;re-opens&quot; the module for additional configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    app.config([&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &apos;$httpProvider&apos;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        function($httpProvider) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $httpProvider.defaults.headers.common[&apos;X-CSRFToken&apos;] =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                &apos;{{ csrf_token&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;escapejs }}&apos;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    ]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endblock&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, we have our editor, let’s build the controller to wire this up. Notice that we depend on two modules now, the base module for our feed and the api module that contains all our &lt;code&gt;$resource&lt;/code&gt; definitions.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;coffeescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.editor&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;example.api&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.photos&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;controller&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;EditController&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$scope&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Post&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;newPost &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-weight:bold&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;save &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;newPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;$save&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        .&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;            # Reset our editor to a new blank post&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;newPost &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-weight:bold&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Previously, in the API Views, we added some permission constraints to prevent other users from modifying someone’s posts. Up until now, it didn’t matter what user were acting as. Now that we want to create users, we need to ensure that we’re authenticated as a valid user (or our API request to create a post will be denied). For this demonstration, there’s a quick hack Django Authentication Backend that automatically logs you in root. Obviously, don’t run this in production or any untrusted environment. This is just to help test our sandbox app without dealing with user registration and authentication.&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;quot;alt&amp;quot;:&amp;quot;Basic Post List - Editor&amp;quot;,&amp;quot;src&amp;quot;:&amp;quot;images/django-rest-framework-and-angular-js/example_editor.png&amp;quot;,&amp;quot;index&amp;quot;:0}&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;error-handling&quot;&gt;Error Handling&lt;/h3&gt;
&lt;p&gt;If you’re following along, have you tried creating a post without a title? We set that as a required field in the Django model, and Django Rest Framework will validate before creating the resource. If you try to create a Post without a title (say, using the API Browser or our new form), you’d see a 400 Bad Request response, with content that the reasons for the failed request. Let’s leverage that response to inform the user.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&quot;This field is required.&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To inform the user, let’s amend our API call. Since we’re using Promises, we can simply add an error callback to capture that response and notify the user by capturing the response in our &lt;code&gt;$scope&lt;/code&gt; which our template can them update to show the messages to the user.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;coffeescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.editor&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;example.api&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.photos&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;controller&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;EditController&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$scope&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;Post&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;newPost &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-weight:bold&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;save &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;newPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;$save&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        .&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;            # Reset our editor to a new blank post&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;newPost &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-weight:bold&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Post&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        .&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;            # Clear any errors&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;errors &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F38BA8&quot;&gt; null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        ,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;rejection&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;errors &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; rejection&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And also update our template to include a handy error display:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;p ng-repeat=&quot;(name, errs) in errors&quot; class=&quot;alert alert-danger&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;strong&amp;gt;{{ name }}&amp;lt;/strong&amp;gt;: {{ errs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;, &apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By leveraging promise APIs, it makes it straightforward to add UI elements to give feedback about the progress (loading indicators, progress bars) by simply chaining them. AngularJS has a fairly complete Promises/A spec ensuring errors (or rejections) fall-through the chain making error handling simpler.&lt;/p&gt;
&lt;p&gt;For this simple demonstration, we just enumerate the errors in bootstrap alert boxes. Because the errors are identified by attribute name, you can easily update your template to show errors in-line next the form fields they correspond with.&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;quot;alt&amp;quot;:&amp;quot;Basic Post List - Error&amp;quot;,&amp;quot;src&amp;quot;:&amp;quot;images/django-rest-framework-and-angular-js/example_error.png&amp;quot;,&amp;quot;index&amp;quot;:0}&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;remove-your-posts&quot;&gt;Remove Your Posts&lt;/h3&gt;
&lt;p&gt;To compliment the editor, we need a way to delete any posts, but only added by the current user. We already instrumented the API to prevent users from trying to modify/delete resources they don’t own. When you’re new to AngularJS, the modular nature makes it difficult to understand how to provide some initial data to the controller. In our case, we need to know who the current user is, so we can control what posts are available to be deleted.&lt;/p&gt;
&lt;p&gt;The key to understanding this is to try to de-compose your controllers into many services/factories (they’re basically synonymous in angular) that handle the actual logic. The controller (much like a view in Django) should really only focus on wiring together different components. In Django, you attempt to integrate as much of your business logic in the model (the fat-model pattern). In Angular, you similarly want to compartmentalize your logic into composable services.&lt;/p&gt;
&lt;p&gt;To add the ability to remove posts, let’s first add a module extending our editor and adding an extra controller for handling the removal. We will depend on a service named &lt;code&gt;AuthUser&lt;/code&gt; which the template will provide with the current user from the Django template context. In this case, it will simply contain a single &lt;code&gt;username&lt;/code&gt; attribute for the current user (our empty if no user). We added two functions on the scope, &lt;code&gt;canDelete&lt;/code&gt; to determine if a given post can be removed by the user, and &lt;code&gt;delete&lt;/code&gt; to remove the post. Both take a &lt;code&gt;post&lt;/code&gt; argument which we will supply in our template.&lt;/p&gt;
&lt;p&gt;Again, we used the Promises interface of the &lt;code&gt;$resource&lt;/code&gt; and only after successful confirmation from the server do we remove the post from our feed view. As above, there’s opportunity for capturing failure results and providing feedback to the user that we’ve skipped in this simple example.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;coffeescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; angular&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.manage&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;example.api&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.app.editor&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;controller&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;DeleteController&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;$scope&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;AuthUser&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;$scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; AuthUser&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;canDelete &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;username &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; AuthUser&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;username&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;delete &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;$delete&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        .&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;            # Remove it from the list on success&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            idx &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;indexOf&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            $scope&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;posts&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;splice&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;idx&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the controller defined, let’s update our Post template to include a close button (with the condition that &lt;code&gt;canDelete&lt;/code&gt; is true).&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;extends&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;editor.html&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %} {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; ng_app %}example.app.manage{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endblock&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; post_header %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;button&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    type=&quot;button&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    class=&quot;close&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    ng-controller=&quot;DeleteController&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    ng-click=&quot;delete(post)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    ng-show=&quot;canDelete(post)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;amp;times;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{{ block&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;super }} {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endblock&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %} {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; js %} {{ block&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;super }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    // Configure the current user&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    var app = angular.module(&apos;example.app.manage&apos;); // Not including a list of dependent modules (2nd parameter to `module`) &quot;re-opens&quot; the module for&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    app.factory(&apos;AuthUser&apos;, function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        return {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            username: &quot;{{ user&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;username&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;escapejs }}&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endblock&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And, when you load the &lt;a href=&quot;http://localhost:8000/manage&quot;&gt;example page&lt;/a&gt;, there’s an &lt;em&gt;‘X’&lt;/em&gt; next to the post by &lt;code&gt;root&lt;/code&gt;, but not next to the post by &lt;code&gt;bob&lt;/code&gt;. Clicking the X calls our API and deletes the post.&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;quot;alt&amp;quot;:&amp;quot;Basic Post List - Removal&amp;quot;,&amp;quot;src&amp;quot;:&amp;quot;images/django-rest-framework-and-angular-js/example_managed.png&amp;quot;,&amp;quot;index&amp;quot;:0}&quot;&gt;&lt;/p&gt;
&lt;p&gt;And with that, we have a simple shared feed for a users to share messages.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Alright, let’s reflect. With a small amount of code (~100 lines of frontend, ~200 lines of backend), leveraging Django Rest Frameowrk (+Django itself) and AngularJS, we were able to quickly create a sample application to create simple postings. DRF makes it simple to export our Django data models through a ReSTful API layer with opportunities to customize the interfaces based on our use cases. AngularJS makes it easier to consume and interact with the API in a manner that is more modular and structured to help us add incremental capability to our web apps without creating spaghetti code.&lt;/p&gt;
&lt;p&gt;All the code referenced in this article is available on a &lt;a href=&quot;https://github.com/kevinastone/django-api-rest-and-angular&quot;&gt;GitHub project&lt;/a&gt;. You’re encouraged to checkout the repository and setup your own working copy to experiment. If you see any mistakes, please leave an Issue (or better yet, a Pull-Request) and I’ll be sure to correct it. If you have any questions, please leave me a comment (or hit me up on Twitter at &lt;a href=&quot;https://twitter.com/kevinastone&quot;&gt;@kevinastone&lt;/a&gt;). I intend to follow this article up with more solutions for DRF+Angular that I’ve come across, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pagination&lt;/li&gt;
&lt;li&gt;Singleton Endpoints (toggle follow/unfollow)&lt;/li&gt;
&lt;li&gt;More Complex Permissions&lt;/li&gt;
&lt;li&gt;Richer Validation&lt;/li&gt;
&lt;li&gt;Testing&lt;/li&gt;
&lt;/ul&gt;
</content:encoded><category>Django</category><category>Python</category><category>AngularJS</category><category>ReST</category></item><item><title>Batteries Included Django</title><link>https://blog.kevinastone.com/batteries-included-django/</link><guid isPermaLink="true">https://blog.kevinastone.com/batteries-included-django/</guid><description>A key benefit to using Python and/or Django for your next project, is the wealth of high quality 3rd party libraries that can help accelerate your development. Python has a rich ecosystem of 3rd party libraries including often offical bindings for an external service. This offers you significant tools to building rich, complex systems based on the available open source solutions. Django&apos;s popularity and architecture provides the opportuntiy to find many existing solutions to common problems. For education and discussion, I thought it would be helpful to enumerate the many libraries we currently use on Subblime to leverage the power of of the Python/Django ecosystem. A what&apos;s in our toolbox sort of post.</description><pubDate>Tue, 16 Jul 2013 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A key benefit to using Python and/or Django for your next project, is the wealth of high quality 3rd party libraries that can help accelerate your development. Python has a rich ecosystem of 3rd party libraries including often offical bindings for an external service. This offers you significant tools to building rich, complex systems based on the available open source solutions. Django’s popularity and architecture provides the opportuntiy to find many existing solutions to common problems.&lt;/p&gt;
&lt;p&gt;For education and discussion, I thought it would be helpful to enumerate the many libraries we currently use on &lt;a href=&quot;http://subblime.com&quot; title=&quot;Subblime&quot;&gt;Subblime&lt;/a&gt; to leverage the power of of the Python/Django ecosystem. A what’s in our toolbox sort of post.&lt;/p&gt;
&lt;h3 id=&quot;requirementstxt&quot;&gt;Requirements.txt&lt;/h3&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;distribute&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=0.6.28&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;mysql-python&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=1.2.4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Django&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=1.5.4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;pillow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;Werkzeug&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;south&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=0.8,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;2.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;sorl-thumbnail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;hamlpy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;requests&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=1.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;djangorestframework&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=2.3.4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;djangorestframework-csv&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;unicodecsv&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# pynliner&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# # Use our fork that leverages lxml, tinycss and cssselect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;https://github.com/subblime/pynliner/tarball/master#egg&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=pynliner&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;cssutils&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Django Enhancements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-extensions&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;1.2.1&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;	# 1.2.0 included a unicode bug (see https://github.com/django-extensions/django-extensions/pull/366)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-model-utils&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-templated-email&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-grappelli&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=2.4.5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-floppyforms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-filter&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=0.7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-braces&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-autoslug&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;pytz&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=2013d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;iso8601&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;python-dateutil&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=1.5,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;2.0	&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# 2.1 includes breaking changes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Deployment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;uwsgi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;fabric&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Caching&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;redis&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;hiredis&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-redis-cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# johnny-cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Patch to skip caching ORDER BY RAND() queries&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;https://github.com/subblime/johnny-cache/tarball/master#egg&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=johnny-cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Django Compressor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-compressor&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=1.3&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;	# Need recent django-compressor for precompile filter support&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;lxml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;BeautifulSoup&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;4.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;cssselect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;cssmin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;tinycss&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;slimit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-sekizai&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Celery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-celery-with-redis&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;	# includes celery and django-celery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;flower&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-celery-transactions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# YouTube&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;gdata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;google-api-python-client&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;oauth2client&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-social-auth&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=0.7.22&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;	# v0.7.22 removed social_auth.signals.pre_update which we now support through pipeline stages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Social&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;facepy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;twitter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Mixpanel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# v0.6 introduced Celery 3.0 support&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;mixpanel-celery&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=0.6.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# For Sentry&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;raven&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=3.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Lettuce&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Need version of Lettuce that supports proper Background sections including xunit support&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;lettuce&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=0.2.18&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Charting&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;numpy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;pandas&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=0.12&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;sqlalchemy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Search&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-haystack&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;pyelasticsearch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;celery-haystack&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;testing-requirementstxt&quot;&gt;Testing Requirements.txt&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;factory_boy&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=1.3,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;2.0	&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Guard against potential 2.0 breaking changes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;mock&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;#pytimecop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Nose&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;nose&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;1.3	&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Nose 1.3 breaks our test discovery somehow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;django-nose&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;coverage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;nose-exclude&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;nose-progressive&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=1.4.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;nosexcover&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;yanc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;xtraceback&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Validaiton&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;pep8&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;flake8&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;lettuce-requirementstxt&quot;&gt;Lettuce Requirements.txt&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;# Make sure you have Chromium installed (or appropriate for your dev platform)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;splinter&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=0.5&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;    # Need 0.5 for PhantomJS support&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt;selenium&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;=2.28&lt;/span&gt;&lt;span style=&quot;color:#9399B2;font-style:italic&quot;&gt;	# Need 2.28 for PhantomJS support&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;basic-includes-for-every-project&quot;&gt;Basic Includes for Every Project&lt;/h2&gt;
&lt;p&gt;These are requirements I type before I even start a project. You’re just likely to need them with some part of your problem set.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/python-imaging/Pillow&quot;&gt;Pillow&lt;/a&gt; (Image Manipulation)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sorl/sorl-thumbnail&quot;&gt;sorl-thumbnail&lt;/a&gt; (Thumbnail Generation)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://south.aeracode.org&quot;&gt;South&lt;/a&gt; (DB Migrations)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://python-requests.org&quot;&gt;Requests&lt;/a&gt; (HTTP client)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://django-rest-framework.org&quot;&gt;Django-Rest-Framework&lt;/a&gt; (ReST API endpoint)&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;datetime-fixes&quot;&gt;DateTime Fixes&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://pytz.sourceforge.net&quot;&gt;pytz&lt;/a&gt; (Timezone support)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://bitbucket.org/micktwomey/pyiso8601&quot;&gt;iso8601&lt;/a&gt; (date formatting and parsing)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://labix.org/python-dateutil&quot;&gt;dateutil&lt;/a&gt; (parsing and translation)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You always end up exporting importing to CSV&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/jdunck/python-unicodecsv&quot;&gt;unicodecsv&lt;/a&gt; (Fixes encoding for CSV import/export)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;general-django-helpers&quot;&gt;General Django Helpers&lt;/h3&gt;
&lt;p&gt;Some utility knife style general enhancements to Django&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/django-extensions/django-extensions&quot;&gt;django-extensions&lt;/a&gt; - runserver_plus + shell_plus management commands (Werkzeug Debugger) - Useful generic model fields (CreationDateTimeField, etc)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/carljm/django-model-utils&quot;&gt;django-model-utils&lt;/a&gt; - Improved Querysets (PassThroughManager, InheritanceManager)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/brack3t/django-braces&quot;&gt;django-braces&lt;/a&gt; - CBV Mixins (instead of ugly decorators)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;django-crud-helpers&quot;&gt;Django CRUD Helpers&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/alex/django-filter&quot;&gt;django-filter&lt;/a&gt; - Don’t write your own query string parameterizations until you’ve tried django-filter&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/brutasse/django-floppyforms&quot;&gt;django-floppyforms&lt;/a&gt; - Better templates for django forms - import floppyforms as forms&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;admin-tools&quot;&gt;Admin Tools&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.grappelliproject.com&quot;&gt;django-grappelli&lt;/a&gt; (nicer skin)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/django-extensions/django-extensions&quot;&gt;django-extensions&lt;/a&gt; (again) - ForeignKeyAutocompleteAdmin makes admin useable for relations (search field instead of dropdown)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;batch-processing&quot;&gt;Batch Processing&lt;/h2&gt;
&lt;h4 id=&quot;celery&quot;&gt;Celery&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://celeryproject.org&quot;&gt;django-celery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/chrisdoble/django-celery-transactions&quot;&gt;django-celery-transactions&lt;/a&gt; - Delays task delivery until transactions commit to eliminate race conditions&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;email&quot;&gt;Email&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/bradwhittington/django-templated-email&quot;&gt;django-templated-email&lt;/a&gt; - Template support for mail subjects, and HTML + Plain versions&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/rennat/pynliner&quot;&gt;pynliner&lt;/a&gt; - inline your CSS for to re-use your css in mail templates&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;cleaner-frontend-code&quot;&gt;Cleaner Frontend Code&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/jessemiller/HamlPy&quot;&gt;HamlPy&lt;/a&gt; (Template loader) - More readable and concise html generation - (Also more version control friendly since it’s line based)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/jezdez/django_compressor&quot;&gt;Django Compressor&lt;/a&gt; - Supports LessCSS and Coffeescript - lxml, BeautifulSoup, cssmin, slimit for minification - NPM Packages: less, jshint, recess, uglify-js, coffee-script&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/ojii/django-sekizai&quot;&gt;django-sekizai&lt;/a&gt; - Adding resources in template fragments on demand&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;haml-templates&quot;&gt;Haml Templates&lt;/h4&gt;
&lt;p&gt;Regular Django HTML&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;p&amp;gt;A bunch of placeholder text about your company.&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;div class=&quot;actions&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;div class=&quot;action-container pull-left&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;a class=&quot;flat-button block-level call-to-action&quot; href=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;join&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;Join our Site!&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;div class=&quot;action-container pull-right&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;a class=&quot;flat-button block-level call-to-action&quot; href=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;login&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;Sign In&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Updated for Haml&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;haml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;p&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    A bunch of placeholder text about your company.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F9E2AF&quot;&gt;.actions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F9E2AF&quot;&gt;    .action-container.pull-left&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;        %&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF&quot;&gt;.flat-button.block-level.call-to-action&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{&apos;href&apos;: &apos;{% url &quot;join&quot; %}&apos;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            Join our Site!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F9E2AF&quot;&gt;    .action-container.pull-right&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;        %&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF&quot;&gt;.flat-button.block-level.call-to-action&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{&apos;href&apos;: &apos;{% url &quot;login&quot; %}&apos;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            Sign In&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;another-example&quot;&gt;Another Example&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;div class=&quot;product-information-container&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;div class=&quot;product-information&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;h4&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; headline %}{{ headline }}{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endif&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; url %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            &amp;lt;p&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                &amp;lt;p&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                    &amp;lt;a href=&quot;{{ url }}&quot;&amp;gt;{{ url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;format_url }}&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                &amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            &amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endif&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;/h4&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        {% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; description %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        &amp;lt;p class=&quot;description&quot;&amp;gt;{{ description }}&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;haml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F9E2AF&quot;&gt;.product-information-container&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F9E2AF&quot;&gt;    .product-information&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;        %&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;h4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            - if headline&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                = headline&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            - if url&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;                %&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;p&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;                    %&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{&apos;href&apos;: &apos;={url}&apos;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                        = url|format_url&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        - if description&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            %p.description&amp;lt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;                = description&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;django-sekizai&quot;&gt;Django Sekizai&lt;/h4&gt;
&lt;p&gt;Sekizai allows you to specify assets (css, js) anywhere in your templates and capture them to include in the base template.&lt;/p&gt;
&lt;p&gt;In Your Base Template:&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;render_block&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;js&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or with Django Compressor Processors (say for Coffeescript)&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;render_block&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;js&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; postprocessor &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&quot;compressor.contrib.sekizai.compress&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Included Fragment Example&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;haml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;- addtoblock &quot;js&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    %script&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;type&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;text/javascript&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;src&apos;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;{% static &quot;js/jquery/jquery.timeago.js&quot; %}&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;- endaddtoblock &quot;js&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Included Fragment Example (Coffeescript)&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;django&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;addtoblock&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;js&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    &amp;lt;script type=&quot;text/coffeescript&quot; src=&quot;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;js/directives/date.coffee&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;{% &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;endaddtoblock&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;js&quot;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; %}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;backend-services&quot;&gt;Backend Services&lt;/h2&gt;
&lt;h3 id=&quot;algorithmicdata-science&quot;&gt;Algorithmic/Data Science&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.numpy.org&quot;&gt;NumPy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://pandas.pydata.org&quot;&gt;Pandas&lt;/a&gt; - Powerful data transformations (esp. for time series data)&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;other-useful-libs&quot;&gt;Other Useful Libs&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.scipy.org&quot;&gt;SciPy&lt;/a&gt; - Algorithms galore&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://nltk.org&quot;&gt;NLTK&lt;/a&gt; (language processing + statistics primitives)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/pymc-devs/pymc&quot;&gt;PyMC&lt;/a&gt; (Bayesian inference)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;caching&quot;&gt;Caching&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/andymccurdy/redis-py&quot;&gt;redis-py&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/pietern/hiredis-py&quot;&gt;hiredis-py&lt;/a&gt; (faster bindings for redis)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/sebleier/django-redis-cache&quot;&gt;django-redis-cache&lt;/a&gt; (cache backend using redis)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;johnny-cache (transparent queryset caching)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;apis-and-authentication&quot;&gt;APIs and Authentication&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/omab/django-social-auth&quot;&gt;django-social-auth&lt;/a&gt; (oauth consumers) - now &lt;a href=&quot;https://github.com/omab/python-social-auth&quot;&gt;python-social-auth&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Google - &lt;a href=&quot;https://developers.google.com/api-client-library/python/&quot;&gt;oauth2client&lt;/a&gt; - &lt;a href=&quot;https://code.google.com/p/google-api-python-client/&quot;&gt;google-api-python-client&lt;/a&gt; - &lt;a href=&quot;https://code.google.com/p/gdata-python-client/&quot;&gt;gdata&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Facebook - &lt;a href=&quot;https://github.com/jgorset/facepy&quot;&gt;facepy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;trackingreporting&quot;&gt;Tracking/Reporting&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/winhamwr/mixpanel-celery&quot;&gt;mixpanel-celery&lt;/a&gt; (background mixpanel tracking)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/getsentry/raven-python&quot;&gt;raven&lt;/a&gt; (for Sentry)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;deployment-and-testing&quot;&gt;Deployment and Testing&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://fabfile.org/&quot;&gt;Fabric&lt;/a&gt; (automation)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://projects.unbit.it/uwsgi&quot;&gt;uWSGi&lt;/a&gt; (WSGi container)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://werkzeug.pocoo.org&quot;&gt;Werkzeug&lt;/a&gt; (Lightweight WSGi framework)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;testing&quot;&gt;Testing&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/FactoryBoy/factory_boy&quot;&gt;Factory-boy&lt;/a&gt; (Fixtures)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.voidspace.org.uk/python/mock/&quot;&gt;mock&lt;/a&gt; (Mocking and Stubbing)&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;feature-tests&quot;&gt;Feature Tests&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://lettuce.it&quot;&gt;lettuce&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/baijum/selenium-python&quot;&gt;selenium&lt;/a&gt; (web browser automation)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://splinter.cobrateam.info&quot;&gt;splinter&lt;/a&gt; (selenium wrapper)&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;validation&quot;&gt;Validation&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/jcrocholl/pep8&quot;&gt;pep8&lt;/a&gt; (coding standards)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://flake8.readthedocs.org/en/latest/&quot;&gt;flake8&lt;/a&gt; (static analysis based on PyFlakes)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;nose-plugins&quot;&gt;Nose Plugins&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/nose-devs/nose&quot;&gt;nose&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/jbalogh/django-nose&quot;&gt;django-nose&lt;/a&gt; - Nose based Test Runner for Django&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://nedbatchelder.com/code/coverage/&quot;&gt;coverage&lt;/a&gt; - Code Coverage&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://bitbucket.org/kgrandis/nose-exclude&quot;&gt;nose-exclude&lt;/a&gt; - Adds –exclude-dir= flags to skip certain dirs&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/erikrose/nose-progressive&quot;&gt;nose-progressive&lt;/a&gt; - Progress bar for test runner&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/cmheisel/nose-xcover&quot;&gt;nosexcover&lt;/a&gt; (worst package name ever) - Code coverage for xunit results (for Jenkins)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/0compute/yanc&quot;&gt;yanc&lt;/a&gt; - Output Color-izer&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/ischium/xtraceback&quot;&gt;xtraceback&lt;/a&gt; - Better Test Tracebacks&lt;/li&gt;
&lt;/ul&gt;
</content:encoded><category>Django</category><category>Python</category><category>Subblime</category></item><item><title>Django Model Behaviors</title><link>https://blog.kevinastone.com/django-model-behaviors/</link><guid isPermaLink="true">https://blog.kevinastone.com/django-model-behaviors/</guid><description>How do we manage the growing complexity of our Django models? Leverage the behavior pattern to keep them compartmentalized into discrete reusable functionality.</description><pubDate>Sun, 12 May 2013 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;As Django projects scale in complexity beyond the neat and tidy tutorial phase, how can we structure our models to keep things manageable? We’re talking 10s to 100s of models, used across numerous views, templates and tests…&lt;/p&gt;
&lt;h2 id=&quot;compositional-model-behaviors&quot;&gt;Compositional Model Behaviors&lt;/h2&gt;
&lt;p&gt;The Compositional Model pattern allows you to manage the complexity of your models through compartmentalization of functionality into manageable components.&lt;/p&gt;
&lt;h4 id=&quot;the-benefits-of-fat-models&quot;&gt;The Benefits of Fat Models&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;li&gt;Single Path&lt;/li&gt;
&lt;li&gt;Separation of Concerns (MVC)&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;without-the-maintenance-cost&quot;&gt;Without the Maintenance Cost&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;DRY&lt;/li&gt;
&lt;li&gt;Readability&lt;/li&gt;
&lt;li&gt;Reusability&lt;/li&gt;
&lt;li&gt;Single Responsibility Principle&lt;/li&gt;
&lt;li&gt;Testability&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;model-behaviors-example&quot;&gt;Model Behaviors Example&lt;/h2&gt;
&lt;h4 id=&quot;traditional-model&quot;&gt;Traditional Model&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    title &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;CharField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;max_length&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;255&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    body &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;TextField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    slug &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;SlugField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    author &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;ForeignKey&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;User&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; related_name&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;posts&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    create_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;DateTimeField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;auto_now_add&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    modified_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;DateTimeField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;auto_now&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    publish_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;DateTimeField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;decomposed-into-discrete-behaviors&quot;&gt;Decomposed into Discrete Behaviors&lt;/h4&gt;
&lt;p&gt;The goal of the behavior pattern is to decompose your models into core, reusable mixins. Create a higher level abstraction than the model field that encapsulates the intended business logic.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;behaviors &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Authorable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Permalinkable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Timestampable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Publishable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Authorable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Permalinkable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Timestampable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Publishable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    title &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;CharField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;max_length&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;255&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    body &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;TextField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;reusable-behaviors&quot;&gt;Reusable Behaviors&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Authorable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    author &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;ForeignKey&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;User&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Meta&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        abstract &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Permalinkable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    slug &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;SlugField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Meta&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        abstract &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Publishable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    publish_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;DateTimeField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Meta&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        abstract &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Timestampable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    create_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;DateTimeField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;auto_now_add&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    modified_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;DateTimeField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;auto_now&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Meta&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        abstract &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;models-are-more-than-just-fields&quot;&gt;Models are more than just Fields&lt;/h2&gt;
&lt;p&gt;Our first cut at common behaviors just captured common fields, but what about everything else models encapsulate?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Properties&lt;/li&gt;
&lt;li&gt;Custom Methods&lt;/li&gt;
&lt;li&gt;Method Overloads (save(), etc…)&lt;/li&gt;
&lt;li&gt;Validation&lt;/li&gt;
&lt;li&gt;Querysets&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;capturing-model-methods&quot;&gt;Capturing Model Methods&lt;/h4&gt;
&lt;p&gt;Let’s extend our traditional fat model with some of these encapsulated busuiness logic&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;property&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; is_published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;publish_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;permalink&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_absolute_url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;blog-post&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (),&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;            &quot;slug&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;slug&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;        })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; pre_save&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; slugify&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;slug&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;slug &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; slugify&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;behaviors-with-methods&quot;&gt;Behaviors with Methods&lt;/h4&gt;
&lt;p&gt;In actuality, these same methods can be generalized and extracted into our behavior models&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Permalinkable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    slug &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;SlugField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Meta&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        abstract &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_url_kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;getattr&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;url_kwargs&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; {}))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; kwargs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;permalink&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_absolute_url&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        url_kwargs &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;get_url_kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;slug&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;slug&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;url_name&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; (),&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; url_kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; pre_save&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; slugify&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;slug&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;slug &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; slugify&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;slug_source&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Publishable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    publish_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;DateTimeField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Meta&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        abstract &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    objects &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PassThroughManager&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;for_queryset_class&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;PublishableQuerySet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; publish_on&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; date&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; date&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;            date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;publish_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; date&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;save&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;property&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; is_published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;publish_date &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;wire-up-the-concrete-model&quot;&gt;Wire up the Concrete Model&lt;/h4&gt;
&lt;p&gt;Since we generalized our behaviors, we need to add some helpers on our concrete models to complete the functionality.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;behaviors &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Authorable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Permalinkable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Timestampable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; Publishable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Authorable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Permalinkable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Timestampable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Publishable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    title &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;CharField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;max_length&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FAB387&quot;&gt;255&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    body &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;TextField&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    url_name &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &quot;blog-post&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;property&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; slug_source&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;title&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;naming-tips&quot;&gt;Naming Tips&lt;/h2&gt;
&lt;p&gt;Use “&amp;lt;verb&amp;gt;-able” naming pattern for behaviors. The “-able” suffix ensures the behaviors are readily identifiable. It also prevents yet another use of the word Mixin. (Don’t worry when the naming deviates from decent english such as in the case of &lt;code&gt;OptionallyGenericRelateable&lt;/code&gt;)&lt;/p&gt;
&lt;h2 id=&quot;custom-queryset-chaining&quot;&gt;Custom Queryset Chaining&lt;/h2&gt;
&lt;p&gt;We all know to chain queryset methods, but what about adding custom manager methods?&lt;/p&gt;
&lt;p&gt;Let’s Find Posts from a Given Author (username1) that are Published (publish_date in the past)&lt;/p&gt;
&lt;h4 id=&quot;queryset-without-encapsulation&quot;&gt;QuerySet without Encapsulation&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;models &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;author__username&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username1&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;publish_date__lte&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;custom-managers&quot;&gt;Custom Managers&lt;/h4&gt;
&lt;p&gt;Let’s create methods on a custom Manager to handle the past-publication date and author filters.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPostManager&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Manager&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;publish_date__lte&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; authored_by&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; author&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;author__username&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    objects &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; BlogPostManager&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; published_posts &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; posts_by_author &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlockPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;authored_by&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username1&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;chaining-our-filters&quot;&gt;Chaining our Filters?&lt;/h4&gt;
&lt;p&gt;What if we try to chain our custom filters?&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;authored_by&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username1&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt;AttributeError&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;QuerySet&apos;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; object&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; has no attribute &lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;published&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;Blogpost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;authored_by&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username1&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;django.db.models.query.QuerySet&apos;&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;solution-custom-querysets&quot;&gt;&lt;em&gt;Solution&lt;/em&gt;: Custom Querysets&lt;/h4&gt;
&lt;p&gt;Leverage &lt;code&gt;PassthroughManager&lt;/code&gt; from &lt;a href=&quot;https://github.com/carljm/django-model-utils&quot;&gt;django-model-utils&lt;/a&gt; to allow chaining of custom manager methods.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; model_utils&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;managers &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PassThroughManager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PublishableQuerySet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;QuerySet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;publish_date__lte&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; AuthorableQuerySet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;QuerySet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; authored_by&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt; author&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;author__username&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPostQuerySet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;AuthorableQuerySet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PublishableQuerySet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Authorable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Permalinkable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Timestampable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; Publishable&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; models&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;Model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    objects &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PassThroughManager&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;for_queryset_class&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;BlogPostQuerySet&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now you can chain custom methods inherited from multiple behaviors.&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; author_public_posts &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;authored_by&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username1&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;Blogpost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;authored_by&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username1&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;example.queryset.BlogPostQuerySet&apos;&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;ensulated-business-logic&quot;&gt;Ensulated Business Logic&lt;/h4&gt;
&lt;p&gt;What’s more legible and maintainable?&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;author__username&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username1&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;publish_date__lte&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;authored_by&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&apos;username1&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;testing-behaviors&quot;&gt;Testing Behaviors&lt;/h2&gt;
&lt;p&gt;Create matching Behavior tests to validate our models.&lt;/p&gt;
&lt;h4 id=&quot;same-benefits-as-for-models&quot;&gt;Same Benefits as for Models&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;DRY&lt;/li&gt;
&lt;li&gt;Readability&lt;/li&gt;
&lt;li&gt;Reusability&lt;/li&gt;
&lt;li&gt;Single Responsibility&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;unit-test-example&quot;&gt;Unit Test Example&lt;/h2&gt;
&lt;p&gt;We can create reusuable test components that validate our behaviors. The list of test mixins then become documentation for the expected role of the model.&lt;/p&gt;
&lt;h4 id=&quot;traditional-test&quot;&gt;Traditional Test&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;test &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; TestCase&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;models &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPostTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_published_blogpost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        blogpost &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;publish_date&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;blogpost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;is_published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertIn&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;blogpost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;converted-to-a-behavior-test-mixin&quot;&gt;Converted to a Behavior Test Mixin&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BehaviorTestCaseMixin&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CBA6F7;font-style:italic&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; get_model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; getattr&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt; &apos;model&apos;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; create_instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        raise&lt;/span&gt;&lt;span style=&quot;color:#FAB387;font-style:italic&quot;&gt; NotImplementedError&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#A6E3A1&quot;&gt;&quot;Implement me&quot;&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PublishableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;BehaviorTestCaseMixin&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_published_blogpost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;utils &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        obj &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;create_instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;publish_date&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;timezone&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertTrue&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;is_published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;assertIn&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;obj&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;model&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;published&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;the-updated-unit-test&quot;&gt;The Updated Unit Test&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; django&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;test &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; TestCase&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;models &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;behaviors&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;tests &lt;/span&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; PublishableTests&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPostTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;PublishableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; create_instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;combine-with-model-specific-tests&quot;&gt;Combine with Model Specific Tests&lt;/h4&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BlogPostTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;PublishableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; AuthorableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PermalinkableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; TimestampableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; TestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; create_instance&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;color:#EBA0AC;font-style:italic&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt; BlogPost&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;objects&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#89B4FA;font-style:italic&quot;&gt; test_blog_specific_functionality&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;        ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;additional-model-testing-tips&quot;&gt;Additional Model Testing Tips&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;a href=&quot;https://github.com/FactoryBoy/factory_boy&quot;&gt;Factory Boy&lt;/a&gt; for creating test instances/fixtures&lt;/li&gt;
&lt;li&gt;Use Inherited TestCases to validate different scenarios&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code catppuccin-mocha&quot; style=&quot;background-color:#1e1e2e;color:#cdd6f4; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; StaffBlogPostTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;PublishableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; AuthorableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PermalinkableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; TimestampableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BaseBlogPostTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    det &lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;setUp&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;user &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; StaffUser&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; AuthorizedUserBlogPostTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt;PublishableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; AuthorableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; PermalinkableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; TimestampableTests&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F9E2AF;font-style:italic&quot;&gt; BaseBlogPostTestCase&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;    det &lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt;setUp&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F38BA8;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#CDD6F4&quot;&gt;user &lt;/span&gt;&lt;span style=&quot;color:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89B4FA&quot;&gt; AuthorizedUser&lt;/span&gt;&lt;span style=&quot;color:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Same behavior expected for Staff or Authorized User)&lt;/p&gt;
&lt;h2 id=&quot;reusability&quot;&gt;Reusability&lt;/h2&gt;
&lt;h4 id=&quot;eventually-build-a-libray-of-behaviors&quot;&gt;Eventually Build a Libray of Behaviors&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Permalinkable&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Publishable&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Authorable&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Timestampable&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;reusable-both-across-our-own-apps-and-shareable-through-the-community&quot;&gt;Reusable both across our own Apps and shareable through the Community&lt;/h4&gt;
&lt;p&gt;More Examples&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Moderatable&lt;/em&gt; - &lt;code&gt;BooleanField(&apos;approved&apos;)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Scheduleable&lt;/em&gt; - (&lt;code&gt;start_date&lt;/code&gt; and &lt;code&gt;end_date&lt;/code&gt; with range queries)&lt;/li&gt;
&lt;li&gt;&lt;em&gt;GenericRelatable&lt;/em&gt; (the triplet of &lt;code&gt;content_type&lt;/code&gt;, &lt;code&gt;object_id&lt;/code&gt; and &lt;code&gt;GenericForeignKey&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Orderable&lt;/em&gt; - &lt;code&gt;PositiveSmallIntegerField(&apos;position&apos;)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;recommended-app-layout&quot;&gt;Recommended App Layout&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;querysets.py&lt;/li&gt;
&lt;li&gt;behaviors.py (uses querysets)&lt;/li&gt;
&lt;li&gt;models.py (composition of querysets and behaviors)&lt;/li&gt;
&lt;li&gt;factories.py (uses models)&lt;/li&gt;
&lt;li&gt;tests.py (uses all, split this into a module for larger apps)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I usually have a &lt;em&gt;common&lt;/em&gt; app that has the shared behaviors, model and behavior test mixins with no dependencies on other apps.&lt;/p&gt;
&lt;h2 id=&quot;limitationspitfalls&quot;&gt;Limitations/Pitfalls&lt;/h2&gt;
&lt;p&gt;Basically the challenges of Django Model Inheritance.&lt;/p&gt;
&lt;h4 id=&quot;leak-abstractions&quot;&gt;Leak Abstractions&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Meta Options don’t implicitly inherit (ordering, etc)&lt;/li&gt;
&lt;li&gt;Manager vs Queryset vs Model (some duplication of logic)&lt;/li&gt;
&lt;li&gt;ModelField options (toggling &lt;code&gt;default=True&lt;/code&gt; vs &lt;code&gt;default=False&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You often need to handle the composition yourself such as merging custom QuerySet classes or combining Meta Options.&lt;/p&gt;
&lt;h2 id=&quot;3rd-party-helpers&quot;&gt;3rd Party Helpers&lt;/h2&gt;
&lt;p&gt;Don’t reinvent the wheel if you don’t have to.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/django-extensions/django-extensions&quot;&gt;Django Extensions&lt;/a&gt; (UUIDField, AutoSlugField, etc)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/carljm/django-model-utils&quot;&gt;Django Model Utils&lt;/a&gt; (already mentioned)&lt;/li&gt;
&lt;li&gt;Filters (&lt;a href=&quot;https://github.com/alex/django-filter&quot;&gt;django-filter&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;test-helpers&quot;&gt;Test Helpers&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Factories (&lt;a href=&quot;https://github.com/FactoryBoy/factory_boy&quot;&gt;factory boy&lt;/a&gt;)
Mocking (&lt;a href=&quot;http://www.voidspace.org.uk/python/mock/&quot;&gt;mock&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;All the example code from this post is available on a &lt;a href=&quot;https://github.com/kevinastone/django-model-behaviors-example&quot;&gt;GitHub Project&lt;/a&gt;.&lt;/p&gt;
</content:encoded><category>Django</category><category>Python</category></item></channel></rss>