Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6754851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:19:50+00:00 2026-05-26T13:19:50+00:00

We have a Django DetailView where we’re displaying an an object ( Site ),

  • 0

We have a Django DetailView where we’re displaying an an object (Site), along with all of it’s related objects (Rooms).

Now, in the template, we could simply iterate over the RelatedManager set:

{% for room in site.room_set.all %}
    do stuff
{% endfor %}

However, the problem for this is that this will pick up all related rooms to a site – however, we need to narrow this set down somewhat by another attribute (let’s call it year) – and this attribute is stored in a Django session variable.

Currently, we’re just using Room.objects.filter(site=some_site, year='2009') in the view code, and that’s fine.

My question is more from curiosity – is there any way to use _set in the template, and still filter down or narrow the set?

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?

Cheers,
Victor

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:19:50+00:00Added an answer on May 26, 2026 at 1:19 pm

    My question is more from curiosity – is there any way to use _set in
    the template, and still filter down or narrow the set?

    Not by default, since there’s no way to pass arguments to the filter call.

    • If it’s functionality that needs to be repeated variably, often, and related to the template: build a custom template tag or filter (which can accept arguments).
    • If it’s functionality that’s specific to a view, code it in the view.
    • If it’s functionality that’s the same across multiple views, build the function in a dry place.

    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.

    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?

    It looks like you can actually just by using a model manager for your reverse related model.

    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():
       ...
    

    Or just on the parent model:

    class Site(models.Model):
        ...
        def year_room_set(self):
            return self.room_set.filter(year=datetime.date.today().year)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Django have any template tags to generate common HTML markup? For example, I
I have Django web-site working on tornado and nginx. I took this tornado launcher
I have django.middleware.csrf.CsrfViewMiddleware in settings.MIDDLEWARE_CLASSES and CSRF verification is working just fine for all
I have django objects: class Event(models.Model): title = models.CharField(max_length=255) event_start_date = models.DateField(null=True, blank='true') ...
I have a Django website in which I want site administrators to be able
I have Django running in Apache via mod_wsgi. I believe Django is caching my
I was trying to setup django . I do have Django-1.1-alpha-1. I was trying
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I have a Django model with a large number of fields and 20000+ table
I have a Django my_forms.py like this: class CarSearchForm(forms.Form): # lots of fields like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.