2
Use ModelForm
with a bit of customaization:
First sub class the ModelChoiceField
from django.utils.safestring import mark_safe
class CustomChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return mark_safe("<img src='%s'/>" % obj.itemImage.url)
Now:
class CatForm(forms.ModelForm):
items = CustomChoiceField(widget=forms.RadioSelect, queryset=MyItems.objects.all())
class Meta:
model = categories
now use this form instead of your catForm
. If you haven’t used ModelForm
before please see the django documentation for details on this.
Source:stackexchange.com