title: How do I pass data from a view to a custom mixin in Django?
body: <p>Two of my views share a substantial amount of code, which makes it hard to maintain. For that reason I decided to move the common code into a custom mixin.</p>
  
  <p>A simplistic example:</p>
  
  <pre><code>class StatisticsMixin(object):
      def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          user_products = Product.objects.filter(user=self.request.user).select_related()
  
          context["total_products"] = user_products.count()
          context["total_products_open"] = user_products.total_new()
  
          return context
  </code></pre>
  
  <p>Now, my views would look like this:</p>
  
  <pre><code>class DashboardView(LoginRequiredMixin, StatisticsMixin, TemplateView):
      model = Product
      template_name = "products/dashboard.html"
  
       def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          user_products = Product.objects.filter(user=self.request.user).select_related()
  
          # Some other code
  
          return context
  
  class AnalyticsView(LoginRequiredMixin, StatisticsMixin, TemplateView):
      model = Product
      template_name = "products/analytics.html"
  
       def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          user_products = Product.objects.filter(user=self.request.user).select_related()
  
          # Some other code
  
          return context
  </code></pre>
  
  <p>The issue is the following in the custom mixin, StatisticsMixin:</p>
  
  <pre><code>user_products = Product.objects.filter(user=self.request.user).select_related()
  </code></pre>
  
  <p>which in different views could be something else.</p>
  
  <p>So, I am trying to find a way for the custom mixin, to get the data from the view and not be hardcoded in it.</p>