I'm trying to create new element using CreateView, and automatic generated form.
class OrderSelected(CreateView):
model = Wynajem
template_name = "order_room.html"
success_url = '/my_orders/'
How to add default value to several Wynajem's fields and check if other fields send by post method are ok?
There's another problem. Default value is argument in url:
url(r'^order/(?P<room>\d+)/$', login_required(OrderSelected.as_view()), name='order')
I want to set one field with room value.
Provide a get_initial
method which returns a dictionary mapping fields to their default values.
class MyView(CreateView):
# ...
def get_initial(self):
return { 'some_field': 'some_default_value' }
This is what the forms framework is for (handling validation) - modify your form, or subclass it and do the modifications necessary before providing it to your class.
class MyForm(FormToSubclass):
def clean_fieldname(self):
fieldname_value = self.cleaned_data.get('fieldname')
# check if field is valid.