Creating Custom Django Model Managers

Creating Custom Django Model Managers

Custer and Pleasonton enjoy a relaxing afternoon in Cuyahoga

In the most basic setup of a Django project, a request is processed by looping through the Model-View-Controller framework and returns a response. The URL directs the request to the View which handles the logic of how to respond. Usually, the View collects information from the database, inserts that information into a template and display it to the end user.

Django uses Model Managers to define the data to returned from the database. Django's default Model Manager—called objects—performs a query of the project's data and applies a method—called all—that returns a result that provides access to all the attributes of the model.  Each interaction with the project is a repeat of this process, with the all method returning access to all the objects within the database.

In addition to all(), the methods filter() and exclude() are available to all Django models through the default manager, each acting on a low-level of your model. If your project uses a model called Plans to store your data, to access all the objects from the Plans model, just apply the all() method to the model:

all_plans = Plans.objects.all()
To access all the data of the model, the all() is applied to the model. Similarily, to focus the results with a particular attribute, apply the filter() method to the manager.

numinous_plans = Plans.objects.filter(metaphysical=True)
To exclude an attribute, use the exclude() method and specify the attribute to apply it to :

illadvised_plans = Plans.objects.exclude(acceptable=False)
In Django it is also possible to create a custom model manager that has access to a higher-level search criteria unique to the model. This is setup in two step within the projects model and then connecting it up with the other parts of the framework the URL, view & template.

Step 1) create the attribute
Returning to the Plans model, add an attribute to the model

Class Plans(model.Model):
    ...
    illadvised = JudgementManager()


The attribute works to provide a link to the JudgementManager that will provide the logic for using the attribute

Step 2  add the JudgementManager class
create the JudgementManager class by placing it just above the Plan Model in your file , create the JudgementManager class

Class JudgementManager(models.Manager):
    def get_queryset(self):
        return super(JudgementManager,self).get_queryset().filter(acceptable=False)


The class inherits the models.Manager class to create the custom manager. The  method then searches for all the illadvised plan and returns them to the user

Step 3 Connect the queryset with the rest of the framework
a) the view
within a class based view define a queryset

class PlanListView(ListView):
    queryset = Plan. Illadvised 


b) the url
url(r'^$', views.post_list, name='post_list'),

c) the template


{% for plan in plans %}
    <h2>   
        <a href="{{ post.get_absolute_url }}">
            {{ post.title }}
        </a>
    </h2>
    <p class="date">
        Don't do this {{ plan.acceptable }} by {{ plant.author }}
    </p>
    {{ plan.body }}
{% endfor %}

Comments

No comments yet.