title: Django. Apply function to queryset
body: <p>I have a queryset and I want to apply function (port_to_app) to its field (dst_port). And then access it from template. How do I do that? I've read about filters and tags, but can't understand the way they work.</p>
  
  <p><strong><em>models.py</em></strong></p>
  
  <pre><code>class FlowQuerySets(models.QuerySet):
       def top_app(self):
          return self.values('dst_port')\
              .annotate(traffic=Sum('bytes')).order_by('-traffic')[:10]
  
  class Flow(models.Model):
      id = models.BigIntegerField(primary_key = True)
      ip_src = models.CharField(max_length=15)
      ip_dst = models.CharField(max_length=15)
      src_port = models.IntegerField()
      dst_port = models.IntegerField()
  
      objects = FlowQuerySets.as_manager()
  
      def port_to_app(self):
          "returns application instead of port using socket module"
          import socket
          if self.dst_port:
              return socket.getservbyport(int(dst_port))
  </code></pre>
  
  <p><strong><em>views.py</em></strong></p>
  
  <pre><code>def top_conversations_app(request):
      top_app = Flow.objects.top_app()
      return render (request, 'flow/home.html', {'top_app': top_app}) 
  </code></pre>
  
  <p><strong><em>home.html</em></strong></p>
  
  <pre><code>{% for p in top_app %}
              {{ p.dst_port }}
              {{ p.traffic | filesizeformat}}
  {% endfor %}
  </code></pre>