In addition to chosen answer:
To use AdvancedModelChoiceField, and still render form via regular widgets, i subclassed django widget and overrided some methods.
first method be optgroups. Replace
for index, (option_value, option_label) in enumerate(chain(self.choices)):
with
for index, (option_value, option_label, obj) in enumerate(chain(self.choices)):
Then, we need to pass that obj to option.
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None, obj=None):
option = super(RadioSelect, self).create_option(name, value, label, selected, index, subindex, attrs)
option['obj'] = obj
return option
In widget template access obj like this:
{{ widget.obj.object_attr }}
And finally, we can use our custom widget and field in our form:
class MyForm(forms.ModelForm):
myfield = AdvancedModelChoiceField(
widget=MyWidget,
queryset=MyModel.objects.all(),
empty_label=None
)