Last active
July 5, 2023 14:01
-
-
Save guillaumepiot/3c10af3ca73036f2757e to your computer and use it in GitHub Desktop.
Django Form - Custom ModelChoiceField label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
class Meta: | |
model = MyModel |
I am sorry, but I am still relatively new to python and Django, what is the purpose of the underscore in line 10 when declaring label?
I think it might be for internationalization
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am sorry, but I am still relatively new to python and Django, what is the purpose of the underscore in line 10 when declaring label?