Advertisement
hellsgate

formfields_callback example

Sep 11th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. class NightclubBookingForm(BaseBookingForm):
  2.     formfield_callback = customise_fields
  3.  
  4.     def __init__(self, *args, **kwargs):
  5.         super(NightclubBookingForm, self).__init__(*args, **kwargs)
  6.         self.fields['booking_start'].widget.attrs['autocomplete'] = 'off'
  7.         self.fields['venue'].queryset = Venue.objects.filter(venue_type__name='Nightclub')
  8.         if 'data' in kwargs:
  9.             booths = Booth.objects.filter(area_layout__area__venue_id=kwargs['data']['venue'])
  10.             booth_choices = []
  11.             for booth in booths:
  12.                 booth_choices.append((booth.id, booth.name))
  13.             self.fields['booth'].widget.choices = booth_choices
  14.         else:
  15.             self.fields['booth'].widget.choices = []
  16.             self.fields['booth'].widget.attrs['disabled'] = 'disabled'
  17.         self.fields['booth'].widget.attrs['class'] = 'select_booth'
  18.  
  19.     class Meta:
  20.         model = NightclubBooking
  21.         fields = ('customer', 'venue', 'booth', 'booking_start', 'pax', 'notes')
  22.  
  23.  
  24. def customise_fields(f):
  25.     """
  26.    Ensure all date & datetime fields in the form have the jquery datepicker
  27.    applied to them
  28.    """
  29.     if isinstance(f, models.DateTimeField):
  30.         datetime_field = forms.DateTimeField(
  31.             widget=forms.DateTimeInput(
  32.                 format='%d/%m/%Y %H:%M',
  33.                 attrs={'class': 'datetimepicker'}
  34.             ),
  35.             input_formats=['%d/%m/%Y', '%d/%m/%Y %H:%M'])
  36.         return datetime_field
  37.     elif isinstance(f, models.DateField):
  38.         date_field = forms.DateField(
  39.             widget=forms.DateInput(
  40.                 format='%d/%m/%Y',
  41.                 attrs={'class': 'datepicker'}
  42.             ),
  43.             input_formats=['%d/%m/%Y'])
  44.         return date_field
  45.     else:
  46.         return f.formfield()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement