title: django custom manager with filter parameter
body: <p>I would like to add a custom manager which can be called from a template, but does not affect the entire model (e.g. admin views) and which listens to a parameter set in the request (user_profile).</p>
  
  <p>The following is what I have so far:</p>
  
  <p>models.py:</p>
  
  <pre><code>class CurrentQuerySet(models.query.QuerySet):
      def current(self):
              return self.filter(id=1) ## this simplified filter test works..
  class CurrentManager(models.Manager):
      use_for_related_fields = True
      def get_query_set(self):
              return CurrentQuerySet(self.model)
      def current(self, *args, **kwargs):
              return self.get_query_set().current(*args, **kwargs)
  </code></pre>
  
  <p>For model B is defined:</p>
  
  <pre><code>    objects = CurrentManager()
  </code></pre>
  
  <p>The template calls:</p>
  
  <pre><code>{% for b in a.b_set.current %}
  </code></pre>
  
  <p>But as soon as I try to pass a parameter to that filter (in this case a date stored on the user-profile) the method does not return any results.</p>
  
  <p>e.g.:</p>
  
  <p>models.py</p>
  
  <pre><code>class CurrentQuerySet(models.query.QuerySet):
      def current(self,my_date): 
              return self.filter(valid_from__lte=my_date) 
  </code></pre>
  
  <p>showA.html</p>
  
  <pre><code>{% for b in a.b_set.current(request.user.get_profile.my_date) %}
  </code></pre>
  
  <p>Instead of passing the parameter from the template, I have also tried to set this in the view.py</p>
  
  <pre><code>@login_required
  def showA(request,a_id):
      my_date = request.user.get_profile().my_date
      a = A.objects.get(id=a_id)
      t = loader.get_template('myapp/showA.html')
      c = RequestContext(request,{'a':a,'my_date':my_date,})
      return HttpResponse(t.render(c))
  </code></pre>
  
  <p>Which part am I missing (or misunderstanding) here?</p>
  
  <p>Thanks</p>
  
  <p>R</p>
  
  <h2>Edit</h2>
  
  <p>Here the models. As mentioned, in this example it's a simple 1:n relationship, but can also be m:n in other cases.</p>
  
  <pre><code>class A(models.Model):
      #objects = CurrentManager()
      a = models.CharField(max_length=200)
      description = models.TextField(null=True,blank=True)
      valid_from = models.DateField('valid from')
      valid_to = models.DateField('valid to',null=True,blank=True)
      def __unicode__(self):
              return self.a
  
  class B(models.Model):
      #objects = models.Manager()
      objects = CurrentManager()
      a = models.ForeignKey(A)
      b = models.CharField(max_length=200)
      screenshot = models.ManyToManyField("Screenshot",through="ScreenshotToB")
      description = models.TextField(null=True,blank=True)
      valid_from = models.DateField('valid from')
      valid_to = models.DateField('valid to',null=True,blank=True)
      def __unicode__(self):
              return self.b
  </code></pre>
  
  <h2>Edit-2</h2>
  
  <p>The accepted answer works for at least one relationship.<br>
  In case of a more nested data model, this method seems not to deliver the expected results:</p>
  
  <p>models.py</p>
  
  <pre><code>class C(models.Model):
      objects = CurrentManager()
      b = models.ForeignKey(A)
      c = models.CharField(max_length=200)
      description = models.TextField(null=True,blank=True)
      valid_from = models.DateField('valid from')
      valid_to = models.DateField('valid to',null=True,blank=True)
      def __unicode__(self):
              return self.c
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>@login_required
  def showA(request,a_id):
      a = A.objects.get(id=a_id)
      my_date = request.user.get_profile().my_date
      b_objects = a.b_set.current(my_date)
      c_objects = b_objects.c_set.current(my_date)
      t = loader.get_template('controltool2/showA.html')
      c = RequestContext(request,{'a':a,'b_objects':b_objects,'c_objects':c_objects,})
      return HttpResponse(t.render(c))
  </code></pre>
  
  <p>This returns the error: <code>'QuerySet' object has no attribute 'c_set'</code>.</p>