How to override a field value before validation?
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', 'pic开发者_JS百科kup_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,
}
Ok, I hope I've now understand the problem correctly:
You have a ReservationForm
which has no user
and no is_reservation
field. Because of that, it doesn't make any sense to set initial={'user': ...}
because user
is not part of that form (and neither is is_reservation
).
On the other hand, you are having a Reservation
model which is populated from some values of the form and an additional user id, as well as an is_reservation
boolean.
So, generally you write something like that:
form = ReservationForm() # you migth want to set inital values here
if request.method == 'POST':
form = ReservationForm(request.POST)
if form.is_valid():
data = form.cleaned_data
reservation = Reservation() # create a new model
# copy the values from the form to your new model
reservation.payment_method = data['payment_methods']
reservation.payment_time = data['payment_time']
# set some additional values for the model (which are required)
reservation.user_id = request.session['user_id'] # for example
reservation.is_reservation = True
# save the reservation (all required fields, including the user_id are now set)
reservation.save()
Please note that this is just a example. I haven't seen your model class yet, so your model might look a bit different. But I think you will be able to adapt it.
Update
I think the following might work (but you will have to exclude the user
and is_reservation
as you already mentioned).
form = ReservationForm(request.POST)
reservation = form.save(commit=False)
reservation.user = request.user
reservation.is_reservation = True
reservation.save()
精彩评论