Skip to content

Instantly share code, notes, and snippets.

View irfanpule's full-sized avatar

Muhammad Irfan irfanpule

  • Indonesia
  • 19:08 (UTC +07:00)
View GitHub Profile
@guillaumepiot
guillaumepiot / custom_modelchoicefield.py
Last active July 5, 2023 14:01
Django Form - Custom ModelChoiceField label
from django import forms
from django.utils.safestring import mark_safe
class CustomModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return mark_safe("My Object custom label <strong>%i</strong>" % obj.id)
class MyForm(forms.ModelForm):
my_field = CustomModelChoiceField(label=_('The form label'), queryset=MyModel.objects.filter(), widget=forms.RadioSelect, empty_label=None)