I've got a form for which I need to set a few values before validation. I'm trying:
if request.method == 'POST':
reservation_form = ReservationForm(request.POST, initial={'is_reservation':True, 'user':request.user})
But it doesn't work. If I exclude the 'user' field from the (model)form, I get a null-constraint error, if I do include it, I get a validation error instead. So either it ignores the initial value because I've excluded the field, or the request.POST data trumps it, even when that value is not posted.
So how am I supposed to do this?
class ReservationForm(ModelForm): # TODO: abstract this and shipment form
service_types = MultipleChoiceField(widget=MultiColumnCheckboxSelect(columns=2), choices=ServiceTypes,
initial=[ServiceTypes.OPEN_TRANS], error_messages={'required': 'Please select at least one service type.'})
payment_methods = MultipleChoiceField(widget=MultiColumnCheckboxSelect(columns=2), choices=PaymentMethods,
error_messages={'required': 'Please select at least one payment method.'})
payment_times = MultipleChoiceField(widget=MultiColumnCheckboxSelect(columns=2), choices=PaymentTimes, required=True,
error_messages={'required': 'Please select at least one payment type.'})
class Meta:
model = Shipment
exclude = ['headline', 'created', 'updated', 'expiry_date', 'status', 'accepted_bid', 'pickup_address', 'dropoff_address', 'billing_address', 'target_price']
widgets = {
'pickup_earliest': TextInput(attrs={'class':'date'}),
'pickup_latest': TextInput(attrs={'class':'date'}),
'dropoff_earliest': TextInput(attrs={'class':'date'}),
'dropoff_latest': TextInput(attrs={'class':'date'}),
'additional_info': WarningTextArea,
}