Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>My question is more from curiosity - is there any way to use _set in the template, and still filter down or narrow the set?</p> </blockquote> <p>Not by default, since there's no way to pass arguments to the filter call.</p> <ul> <li>If it's functionality that needs to be repeated <strong>variably</strong>, often, and related to the template: build a custom template tag or filter (which can accept arguments).</li> <li>If it's functionality that's specific to a view, code it in the view.</li> <li>If it's functionality that's the same across multiple views, build the function in a dry place. </li> </ul> <p>For #3, there are so many factors that determine where this code should go that there's no general solution. You could import a function into your views? Use a model manager? A model instance method? context processor? etc. </p> <blockquote> <p>Could you write a custom Model Manager to do this, so that _set will only ever return objects for the current year? Or is there some other way?</p> </blockquote> <p>It looks like you can actually just by using a model manager for your reverse related model.</p> <pre><code>class RoomManager(models.Manager): def current_year(self): return self.get_queryset().filter(year=datetime.date.today().year) for room in site.room_set.current_year(): ... </code></pre> <p>Or just on the parent model:</p> <pre><code>class Site(models.Model): ... def year_room_set(self): return self.room_set.filter(year=datetime.date.today().year) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload