Skip to content

Instantly share code, notes, and snippets.

@grizmio
Created July 24, 2023 03:33
Show Gist options
  • Save grizmio/75b3bbed860c14b7bd4952e9788fd701 to your computer and use it in GitHub Desktop.
Save grizmio/75b3bbed860c14b7bd4952e9788fd701 to your computer and use it in GitHub Desktop.
Django ModelChoiceField label to "title"
from django.template.defaultfilters import title
class LabelToTitle:
def __init__(self):
self.label = title(self.label)
class FooBarModelChoiceField(forms.ModelChoiceField, LabelToTitle):
def label_from_instance(self, obj):
return obj.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment