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 4030026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:25:11+00:00 2026-05-20T11:25:11+00:00

In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,)

  • 0

In my Django App I have the following model:

class SuperCategory(models.Model):
  name = models.CharField(max_length=100,)
  slug = models.SlugField(unique=True,)

class Category(models.Model):
  name            = models.CharField(max_length=100,)
  slug            = models.SlugField(unique=True,)
  super_category  = models.ForeignKey(SuperCategory)

What I’m trying to accomplish in Django’s Admin Interface is the rendering of Category using widget CheckboxSelectMultiple but with Category somehow grouped by SuperCategory, like this:


Category:

Sports: <- Item of SuperCategory
[ ] Soccer <- Item of Category
[ ] Baseball <- Item of Category
[ ] …

Politics: <- Another item of SuperCategory
[ ] Latin America
[ ] North america
[ ] …


Does anybody have a nice suggestion on how to do this?

Many thanks.

  • 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-20T11:25:11+00:00Added an answer on May 20, 2026 at 11:25 am

    After some struggle, here is what I got.

    First, make ModelAdmin call a ModelForm:

    class OptionAdmin(admin.ModelAdmin):
    
       form = forms.OptionForm
    

    Then, in the form, use use a custom widget to render:

    category = forms.ModelMultipleChoiceField(queryset=models.Category.objects.all(),widget=AdminCategoryBySupercategory)    
    

    Finally, the widget:

    class AdminCategoryBySupercategory(forms.CheckboxSelectMultiple):
    
         def render(self, name, value, attrs=None, choices=()):
             if value is None: value = []
             has_id = attrs and 'id' in attrs
             final_attrs = self.build_attrs(attrs, name=name)
             output = [u'<ul>']
             # Normalize to strings
             str_values = set([force_unicode(v) for v in value])
             supercategories = models.SuperCategory.objects.all()
             for supercategory in supercategories:
                 output.append(u'<li>%s</li>'%(supercategory.name))
                 output.append(u'<ul>')
                 del self.choices
                 self.choices = []
                 categories = models.Category.objects.filter(super_category=supercategory)
                 for category in categories:
                     self.choices.append((category.id,category.name))
                 for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
                     if has_id:
                         final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                         label_for = u' for="%s"' % final_attrs['id']
                     else:
                         label_for = ''
                     cb = forms.CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
                     option_value = force_unicode(option_value)
                     rendered_cb = cb.render(name, option_value)
                     option_label = conditional_escape(force_unicode(option_label))
                     output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label))
                 output.append(u'</ul>')
                 output.append(u'</li>')
             output.append(u'</ul>')
             return mark_safe(u'\n'.join(output))
    

    Not the most elegant solution, but hey, it worked.

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

Sidebar

Related Questions

I have the following model in my Django app: class Group(models.model): name=models.CharField(max_length=30) users=Models.ManyToManyField(User) In
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I have an existing app with the following model class Contact(models.Model): lastname = models.CharField(max_length=200)
I have the following two models class ModelOne(models.Model): mod_desc = models.CharField(max_length=25) is_active = models.BooleanField(default=True)
Lets say, I have following models in my Django app. class EventGroup name =
I have the following django model with an ImageField: class Pics(models.Model): def file_path(instance, filename):
I've got the following model in my Django app: class Image(models.Model): image = models.ImageField(
I have the following models in my Django app. How can I from the
I have a django app with models as follows: A Question model An Answer

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.