Created
May 2, 2019 16:31
-
-
Save jordotech/838f28e8d57b548d4cd8fa1704f0e744 to your computer and use it in GitHub Desktop.
Django form db hits, move to __init__
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
# 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