<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MeetParikh01</title>
    <description>The latest articles on DEV Community by MeetParikh01 (@meetparikh01).</description>
    <link>https://dev.to/meetparikh01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F288533%2F2fa354aa-5424-4930-8768-1e57cfd8832e.png</url>
      <title>DEV Community: MeetParikh01</title>
      <link>https://dev.to/meetparikh01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meetparikh01"/>
    <language>en</language>
    <item>
      <title>Definitive Guide for Adding Custom Model Managers in Django</title>
      <dc:creator>MeetParikh01</dc:creator>
      <pubDate>Thu, 30 Apr 2020 07:22:59 +0000</pubDate>
      <link>https://dev.to/botreetechnologies/definitive-guide-for-django-managers-34ba</link>
      <guid>https://dev.to/botreetechnologies/definitive-guide-for-django-managers-34ba</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yRan9vxv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/definitive-guide-for-django-managers.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yRan9vxv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/definitive-guide-for-django-managers.png" alt="Django managers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a manager?
&lt;/h2&gt;

&lt;p&gt;A manager is an interface through which database query operations are provided to django models. It is some kind of 'gate' between application and database&lt;/p&gt;

&lt;p&gt;Hence,  there exists at least one manager for every model in a django application.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this case, django adds the default manager with the name &lt;strong&gt;objects&lt;/strong&gt; is used.&lt;/p&gt;

&lt;p&gt;We can even rename the manager's name on a pre model basis . So let us consider a Student Model.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here, we can access Student model queries like  &lt;strong&gt;Student.student.all()&lt;/strong&gt; where &lt;strong&gt;student&lt;/strong&gt; is the manager's name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of using managers in your code:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Clean code.&lt;/li&gt;
&lt;li&gt;Efficient code.&lt;/li&gt;
&lt;li&gt;Maintainable code.&lt;/li&gt;
&lt;li&gt;Writing common query code for the model which are reusable. (DRY rule i.e. Don’t repeat yourself)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to write custom managers?
&lt;/h3&gt;

&lt;p&gt;A custom Manager is created by extending a base &lt;strong&gt;Manager&lt;/strong&gt; class  in a particular model and by instantiating  the created custom manager within the model. &lt;/p&gt;

&lt;p&gt;We have created  a custom manager by name &lt;strong&gt;StudentManager&lt;/strong&gt; and thus, our managers looks like -&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Thus, to access a Student Model with different queries defined in the StudentManager, it can be done like this -&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Though in the given examples we have returned the queryset,  one can return anything like a list, dictionary, attribute value etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modifying a manager initial QuerySet
&lt;/h3&gt;

&lt;p&gt;A Manager’s base QuerySet returns all objects in the system. For example, in our &lt;strong&gt;Student&lt;/strong&gt; Model we get all the objects using &lt;strong&gt;Student.student.all()&lt;/strong&gt; but if we want to modify the initial queryset  then we need to override a &lt;strong&gt;Manager’s&lt;/strong&gt; base &lt;strong&gt;QuerySet&lt;/strong&gt; by overriding the &lt;strong&gt;manager.get_queryset()&lt;/strong&gt; method. &lt;strong&gt;get_queryset()&lt;/strong&gt; should return a &lt;strong&gt;QuerySet&lt;/strong&gt; with the properties we require.&lt;/p&gt;

&lt;p&gt;So here we have created two managers for our &lt;strong&gt;Student&lt;/strong&gt; Model by name &lt;strong&gt;MaleManager&lt;/strong&gt; and &lt;strong&gt;FemaleManager&lt;/strong&gt; and instantiated them by name &lt;strong&gt;male&lt;/strong&gt; and &lt;strong&gt;female&lt;/strong&gt;, so our &lt;strong&gt;MaleManager&lt;/strong&gt; and &lt;strong&gt;FemaleManager&lt;/strong&gt; looks as and their access like.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  ProjectStatusSerializerCalling custom QuerySet method from the manager
&lt;/h3&gt;

&lt;p&gt;Most of the methods from the standard QuerySet are directly accessible from the manager but if we want to add methods that need to be accessible as attributes then we need to define Custom Queryset.&lt;/p&gt;

&lt;p&gt;We need to do this because the methods defined in the manager are not chainable.&lt;/p&gt;

&lt;p&gt;For example, in our &lt;strong&gt;StudentManager&lt;/strong&gt; we have a method by name &lt;strong&gt;get_males()&lt;/strong&gt; if we try to chain it with &lt;strong&gt;get_students_age_less_or_equal()&lt;/strong&gt; then it will throw error like this -&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So, to make it work, we will define our own QuerySets Method  and using that method in a seperate manager named as &lt;strong&gt;StudentModifyManager&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now as in our models we have instantiate our &lt;strong&gt;StudentModifyManager&lt;/strong&gt; as &lt;strong&gt;modify&lt;/strong&gt; named object, so we can use the chainable methods as -&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Creating a manager with QuerySet Methods.
&lt;/h3&gt;

&lt;p&gt;While calling  &lt;strong&gt;StudentQuerySet&lt;/strong&gt; Methods, we need to &lt;strong&gt;duplicate&lt;/strong&gt; its methods in our &lt;strong&gt;StudentModifyManager&lt;/strong&gt;. If we don’t want to duplicate those methods then we can create a manage with queryset methods.&lt;/p&gt;

&lt;p&gt;In our &lt;strong&gt;Student&lt;/strong&gt; Model we have created a manager with object &lt;strong&gt;query as&lt;br&gt;
query&lt;/strong&gt; = &lt;strong&gt;StudentQuerySet.as_manager()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, we can use that manager as &lt;strong&gt;Student.query.male().get_sem3_students()&lt;/strong&gt; and can get output according to it.&lt;/p&gt;

&lt;p&gt;Thus, here the &lt;strong&gt;StudentQuerySet.as_manager()&lt;/strong&gt; is virtually identical to the &lt;strong&gt;StudentModifyManager&lt;/strong&gt; .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;from_queryset&lt;/strong&gt; method: classmethod from_queryset(queryset_class)&lt;/p&gt;

&lt;p&gt;If we want the custom &lt;strong&gt;Manager&lt;/strong&gt; and the custom &lt;strong&gt;QuerySet&lt;/strong&gt; for the advanced usage then it can be done by &lt;strong&gt;Manager.from_queryset()&lt;/strong&gt; which returns the subclass of the base class with a copy of the custom Queryset methods.&lt;/p&gt;

&lt;p&gt;So in our Student model, we have done it by creating a &lt;strong&gt;custom&lt;/strong&gt; object in the model as &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;custom = StudentModifyManager.from_queryset(StudentQuerySet)()&lt;/strong&gt;  where the &lt;strong&gt;StudentModifyManager&lt;/strong&gt; is our custom Manager and the &lt;strong&gt;StudentQuerySet&lt;/strong&gt; is our custom QuerySet &lt;/p&gt;

&lt;p&gt;Now looking at the output in our shell -&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Django managers ensure that you write code which is easily maintained and adhere to the DRY principle. They allow keeping queries at a centralized location so that all the queries come from a single location instead of flying in from everywhere.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://www.botreetechnologies.com/django-development"&gt;Click here for more details…&lt;/a&gt;
&lt;/h3&gt;

</description>
    </item>
  </channel>
</rss>
