3👍
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
- [Django]-Django choices tutorial
- [Django]-Loading a pickled list just once – Django\Python
- [Django]-What is the best way to increment an integer at a certain rate over time using a Django powered site and SQLite?
- [Django]-How do I set subdirectory in nginx with Django
0👍
The problem is that Django doesn’t provide a simple way to add extra attributes to form fields. In particular, choice fields values are constrained to be simple strings.
This solution provides a way to provide string-like objects for choice values as well as extra attributes.
Consider a simple model with several choices as values.
# models.py
from django import models
_value_choices = ['value#1', 'value#2', 'value#3'] # the literal values for users
# list creates a list rather than generator
# enumerate provides integers for storage
VALUE_CHOICES = list(zip(enumerate(_value_choices)))
class MyModel(models.Model):
value = models.PositiveSmallIntegerField(choices=VALUE_CHOICES)
We create a ModelForm
as usual.
# forms.py
from django import forms
from . import models
class MyModel(forms.ModelForm):
class Meta:
model = models.MyModel
fields = ['value']
widgets = {
'value': forms.RadioSelect(),
}
Now suppose we have the following template:
{# template #}
{% for radio in field %}
<li>
<div>
{{ radio.tag }}
<label for="{{ radio.id_for_label }}">
</div>
</li>
{% endfor %}
The problem we now have before us is to expand the template so that each label can have extra text that is associated with the choice.
The solution consists of two parts:
I – use a special class for the choice values which can be coerced to a string;
II – create a deconstruct method on how to convert from the stored value to a full object.
I: Creating a special class for visible choice values
This is straightforward.
class RadioChoice:
def __init__(self, label, arg1, arg2): # as many as you want
self.label = label
self.arg1 = arg1
self.arg2 = arg2
def __str__(self): # only the label attribute is official
return self.label
Now rewrite _value_choices
above to use this class
_value_choices = [
RadioChoice('value#1', 'value_arg1_1', 'value_arg1_2'),
RadioChoice('value#2', 'value_arg2_1', 'value_arg2_2'),
RadioChoice('value#3', 'value_arg3_1', 'value_arg3_2'),
]
Include the new attributes in your template.
{% for radio in field %}
<li>
<div>
{{ radio.tag }}
<label for="{{ radio.id_for_label }}"><span>{{ radio.choice_label }}</span> <span>{{ radio.choice_label.arg1 }}</span></label>
<small>{{ radio.choice_label.arg2 }}</small>
</div>
</li>
{% endfor %}
Now test to ensure it works as expected.
II: Add the deconstruct()
method and run migrations
Once you are sure it works correctly you will need to create a new migration for the change in the models.
class RadioChoice:
def __init__(self, label, arg1, arg2): # as many as you want
self.label = label
self.arg1 = arg1
self.arg2 = arg2
def __str__(self): # only the label attribute is official
return self.label
def deconstruct(self):
# https://docs.djangoproject.com/en/3.1/topics/migrations/#adding-a-deconstruct-method
# you must return three arguments: path (to the module from the project root), args and kwargs
path = "app_name.models.RadioChoice"
args = (self.label, self.arg1, self.arg2)
kwargs = dict()
return path, args, kwargs
Finally, run python manage.py makemigrations && python manage.py migrate
.