Q:

How to cache query results in django and use it?

I have a small request, the data from which is transferred to <select>. I use a paginator and this selector makes a request every page.So I want to cache it and try to update every 10 minutes, for example.How do I save a cache and how do I pass it to a template so that the selector works?
views.py
contractors = Contractors.objects.values_list('name', flat='True')

HTML-code
<select name="contractorName" class="form-control" id="id_contractorName">
<option value="" selected=""></option>
{% for contractor in contractors %}
<option value="{{ contractor }}">{{ contractor }}</option>
{% endfor %}
 </select>

A:

There are many ways you can do that based on django cache framework. 
Here is an example on how you can make a template fragment cache:
{% load cache %}

{% cache 600 contractors %}
    <select name="contractorName" class="form-control" id="id_contractorName">
    <option value="" selected=""></option>
    {% for contractor in contractors %}
    <option value="{{ contractor }}">{{ contractor }}</option>
    {% endfor %}
    </select>
{% endcache %}

Or you can also use low level caching API from django:
from django.core.cache import cache

...

contractors = cache.get('contractors')
if not contractors:
     contractors = list(Contractors.objects.values_list('name', flat='True'))
     cache.set('contractors', contractors, 600)

