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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:31:24+00:00 2026-05-21T03:31:24+00:00

I have a Location model and an associated form that displays the location field

  • 0

I have a Location model and an associated form that displays the “location” field as a bunch of radio buttons (using the form queryset to display the values). There are a small number of locations, but they need to be dynamic. I’d like to display the location description next to each radio box option so users have a bit more info on the locations.

Pretend this is a list of radio buttons, this is what I’d like it to look like:

<> East – This location is east. <> West – This is the west location! <> North – This is the north location

I have a model similar to the following:

class Location(models.Models):
    location = models.CharField(max_length=50)
    description = models.TextField(blank=True, null=True)

And a form as such:

class LocationForm(forms.Form):
    location = ExtraModelChoiceField(
               widget=RadioSelect(renderer=ExtraHorizRadioRenderer), 
               queryset = models.Locations.objects.filter(active=True))

I can’t seem to find a good way to render the form so I can display the description along with each select option. I’ve done a lot of overriding, but am not having too much luck.

MY ATTEMPT TO SOLVE (BUT NO LUCK YET):

From what I gather, normally if a queryset is provided on the form field, the Django form logic translates that into a choices tupal of tupals. Each “subtupal” contains an id and label that is displayed when it is rendered. I’m trying to add a third value to those “subtupals” which would be a description.

I’ve defined a custom renderer to display my radio buttons horizontally and to pass in my custom choices.

class ExtraHorizRadioRenderer(forms.RadioSelect.renderer):
    def render(self):
        return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))

    def __iter__(self):
        for i, choice in enumerate(self.choices):
            yield ExtraRadioInput(self.name, self.value, 
                                  self.attrs.copy(), choice, i)

    def __getitem__(self, idx):
        choice = self.choices[idx] # Let the IndexError propogate
        return ExtraRadioInput(self.name, self.value, 
                               self.attrs.copy(), choice, idx)

I’ve overridden the Django RadioInput class so I can add the description information that I need to display next to the Radio Buttons.

class ExtraRadioInput(forms.widgets.RadioInput):

    def __init__(self, name, value, attrs, choice, index):
        self.name, self.value = name, value
        self.attrs = attrs
        self.choice_value = force_unicode(choice[0])
        self.choice_label = force_unicode(choice[1])
        self.choice_description = force_unicode(choice[2])   # <--- MY ADDITION; FAILS
        self.index = index

    def __unicode__(self):
        if 'id' in self.attrs:
            label_for = ' for="%s_%s"' % (self.attrs['id'], self.index)
        else:
            label_for = ''
        choice_label = conditional_escape(force_unicode(self.choice_label))
        return mark_safe(u'<label%s>%s %s</label>' % (
             label_for, self.tag(), choice_label))

    def tag(self):
        if 'id' in self.attrs:
            self.attrs['id'] = '%s_%s' % (self.attrs['id'], self.index)
        final_attrs = dict(self.attrs, type='radio', name=self.name, 
                      value=self.choice_value)
        if self.is_checked():
            final_attrs['checked'] = 'checked'
        return mark_safe(
           u'<input%s /><span class="description">%s</span>' % \
           (flatatt(final_attrs),self.choice_description ))  # <--- MY ADDTIONS

I’ve also overridden the following two Django classes hoping to pass around my modified choices tupals.

class ExtraModelChoiceIterator(forms.models.ModelChoiceIterator  ):    

    def choice(self, obj): 
        if self.field.to_field_name:
            key = obj.serializable_value(self.field.to_field_name)
        else:
            key = obj.pk

        if obj.description:   # <-- MY ADDITIONS
            description = obj.description
        else:
            description = ""
        return (key, self.field.label_from_instance(obj),description)


class ExtraModelChoiceField(forms.models.ModelChoiceField):

    def _get_choices(self):
        if hasattr(self, '_choices'):
            return self._choices
        return ExtraModelChoiceIterator(self)  # <-- Uses MY NEW ITERATOR

Using the approach above, I can’t seem to be able to pass around my 3-value tupal. I get a “tuple index out of range” failure (up where I mark FAILURE above) indicating that somehow my tupal does not have the extra value.

Does anyone see a flaw in my logic, or more generally have an approach to displaying a description next to a list of choices using a widget?

Thanks for reading. Any comments are much appreciated.
Joe

  • 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-21T03:31:24+00:00Added an answer on May 21, 2026 at 3:31 am

    Sorry to answer my own question, but I think I have a method to do this. As always, it appears to be simpler than I was making it before. Overriding the label_from_instance method on an extended ModelChoiceField seems to allow me to access the model object instance to be able to print out extra information.

    from django.utils.encoding import smart_unicode, force_unicode
    
    class ExtraModelChoiceField(forms.models.ModelChoiceField):
    
        def label_from_instance(self, obj):
            return mark_safe(
                "<span>%s</span><span class=\"desc\" id=\"desc_%s\">%s</span>" % (
                mart_unicode(obj), obj.id, smart_unicode(obj.description),))
    
    
    class HorizRadioRenderer(forms.RadioSelect.renderer):
        # for displaying select options horizontally. 
        # https://wikis.utexas.edu/display/~bm6432/Django-Modifying+RadioSelect+Widget+to+have+horizontal+buttons
        def render(self):
            return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))
    
    
    class LocationForm(forms.Form):
        location = ExtraModelChoiceField(widget=forms.RadioSelect(renderer=HorizRadioRenderer),
            queryset=models.Location.objects.filter(active=True))
    

    If you know of a better approach, I’d be excited to see it. Otherwise, this will have to do. Thanks for reading. Hope this saves someone the frustration I had.

    Joe

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

Sidebar

Related Questions

I have a model that looks like this: Performance - Location - Event -
I have a model of events that has various information such as date, location,
So I have a location search field that I want to accept pretty much
I have two columns in a location model that should both associate with the
I have a User entity that has a Current Location field (city and country).
I have model with a location, which itself has a latitude and longitude. What
I have a Meeting model: class Meeting < ActiveRecord::Base has_one :location, :class_name => MeetingLocation,
I have three models that I'm trying to setup: Location/Venues, Categories, and Neighborhoods. A
I have a Location that can have Events. I want to have an upcoming_events
I have successfully got Thinking Sphinx working with Geolocation on an associated model. Happy

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.