I see that this is done in two ways.
You can create your own widget for this field, which will display the attributes of each model. It is very possible, but now I do not have time to write everything.
Another, faster method is to add a function to your form that handles this and returns the data you need.
class ChooseProductTypeForm(ModelForm): def get_my_modelfield(self): for option in self.fields['my_modelfield'].queryset: yield { 'image': option.image, 'title': option.title, 'description': option.description }
Basically, this causes the database to exit the template and simply use the easy-to-use form function to return the data you need. If the form field ever changes, you no longer scatter the corrective patterns, but simply update this function.
The disadvantage of this is that you cannot simply scroll through the fields in your form, you will need to display each of them explicitly. Of course, you can also play with __iter__ in your form, but that might be too hacky. Not sure if you are dealing entirely here :)
source share