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

  • Home
  • SEARCH
  • 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 6191813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:49:58+00:00 2026-05-24T02:49:58+00:00

I have a model called Subtopic. One of my templates runs a forloop on

  • 0

I have a model called Subtopic. One of my templates runs a forloop on an object, returning a different field for each cell of a table row.

Two of the table cells look up a field which is a ManytoMany foreign key, both to the same foreign model, Resource. I want each to display different results, based on the value of a boolean field within the Resource model.

What you see below is currently working fine, but doesn’t attempt to filter by the boolean field.

models.py:

class ICTResourceManager(models.Manager):
    def get_query_set(self):
        return super(ICTResourceManager, self).get_query_set().filter('is_ict': True)

class NonICTResourceManager(models.Manager):
    def get_query_set(self):
        return super(NonICTResourceManager, self).get_query_set().filter('is_ict': False)

class Resource(models.Model):
    subtopics = models.ManyToManyField(Subtopic)
    external_site = models.ForeignKey(ExternalSite)
    link_address = models.URLField(max_length=200, unique=True, verify_exists=False)
    requires_login = models.BooleanField()
    is_ict = models.BooleanField()
    flags = models.ManyToManyField(Flag, blank=True)
    comment = models.TextField()
    def __unicode__(self):
        return u'%s %s' % (self.external_site, self.link_address)
    objects = models.Manager()
    ict_objects = ICTResourceManager()
    nonict_objects = NonICTResourceManager()

    class Meta:
        ordering = ['external_site', 'link_address']

views.py:

def view_ks5topic(request, modulecode, topicshortname):
    listofsubtopics = Subtopic.objects.filter(topic__module__code__iexact = modulecode, topic__shortname__iexact = topicshortname)
    themodule = Module.objects.get(code__iexact = modulecode)
    thetopic = Topic.objects.get(module__code__iexact = modulecode, shortname__iexact = topicshortname)
    return render_to_response('topic_page.html', locals())

My template:

        {% for whatever in listofsubtopics %}
        <tr>
            <td>
                {{ whatever.objective_html|safe }}
                <p>
                {% if request.user.is_authenticated %}
                    {% with 'objective' as column %}
                    {% include "edit_text.html" %}
                    {% endwith %}
                {% else %}
                {% endif %}
            </td>
            <td>
                {% regroup whatever.resource_set.all by external_site.name as resource_list %}
                {% for external_site in resource_list %}
                    <h4>{{ external_site.grouper }}</h4>
                    <ul>
                        {% for item in external_site.list %}
                        <li><a href="{{ item.link_address }}">{{ item.comment }}</a></li>
                        {% endfor %}
                    </ul>
                {% endfor %}
            </td>
        </tr>        
        {% endfor %}

As you can see, I’ve added extra managers to the model to do the filtering for me, but when I replace the appropriate lines in the template, I just get blanks. I have tried: for external_site.ict_objects in resource_list and for item.ict_objects in resource_list and <a href="{{ item.ict_objects.link_address }}">. If this were in the view I could probably do the filter just by .filter('is_ict': True), but with this being inside a forloop I don’t know where to do the filtering.

I also tried writing regroup whatever.resource_set.filter('is_ict': True) in the template, but the syntax for regrouping seems to use resource_set.all rather than resource_set.all() (and I don’t know why) so the filter text doesn’t work here.

  • 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-24T02:49:58+00:00Added an answer on May 24, 2026 at 2:49 am

    Turns out it was possible to do it using a custom template filter. The original efforts to filter within the template weren’t working, given that as is well documented the template language is not a fully-fledged python environment. My original question remains open for anyone who knows an alternative method that more directly addresses the question I was originally asking, but here’s how I did it:

    myapp_extras.py:

    from django import template
    
    register = template.Library()
    
    def ict(value, arg):
        "filters on whether the is_ict Boolean is true"
        return value.filter(is_ict=arg)
    
    register.filter('ict', ict)
    

    My template, note the use of the custom filter in line 2:

    <td>
        {% regroup whatever.resource_set.all|ict:1 by external_site.name as resource_list %}
        {% for external_site in resource_list %}
            <h4>{{ external_site.grouper }}</h4>
            <ul>
            {% for item in external_site.list %}
                <li><a href="{{ item.link_address }}">{{ item.comment }}</a></li>
            {% endfor %}
            </ul>
        {% endfor %}
    </td>
    

    After this I was able to remove the additional custom managers from the model.

    One further question, when filtering for the boolean is_ict field, I found that I had to use filter(is_ict=1) and filter(is_ict=0). Is that the only way to refer to a True or False value?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called shipments. I added some columns to the shipments table
I have a model called User , and another called Run . Each user
I have a model called user. I want to show the password field just
I have a model called Notifications, which basically acts as a join table between
So I have a model called Image that belongs_to :user. Each user has a
Say I have a model called Fruit and a query is returning all the
I have a model called company and one called user, and User belongs to
I have a model called Shop and one called Brand. shop :has_many_brands brands: belongs_to_shop
I have a Model called statistics which has a value field that contains Goals
I have a model called Question and another one called Answer. class Question(models.Model): text

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.