Skip to content

Instantly share code, notes, and snippets.

@jordotech
Created May 2, 2019 16:31
Show Gist options
  • Save jordotech/838f28e8d57b548d4cd8fa1704f0e744 to your computer and use it in GitHub Desktop.
Save jordotech/838f28e8d57b548d4cd8fa1704f0e744 to your computer and use it in GitHub Desktop.
Django form db hits, move to __init__
# Bad
class ShipForm(forms.Form):
printer_station = forms.CharField(
label=_('Print Station'),
widget=forms.Select(choices=get_printer_station_choices()), # Bad, this method hits the db during reload
required=False)
# Good
class ShipForm(forms.Form):
def __init__(self, *args, **kwargs):
self.base_fields['printer_station'].widget=forms.Select(choices=get_printer_station_choices())
super(ShipForm, self).__init__(*args, **kwargs)
printer_station = forms.CharField(
label=_('Print Station'),
required=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment