Django. How to set default option use generic CreateView -
model:
class mymodel(model.models) status = models.foreignkey(statuses, on_delete=models.protect, null=true, blank=true) views:
class myviewcreate(createview): model = mymodel form_class = myfrom template_name = 'mymodel_form.html' forms:
class myform(modelform): class meta: model = mymodel fields = '__all__' how can in form set default or first value on option list >
you can use initial this:
class myviewcreate(createview): model = mymodel form_class = myfrom initial = {'status': '0'} template_name = 'mymodel_form.html' or this:
class myviewcreate(createview): model = mymodel form_class = myfrom template_name = 'mymodel_form.html' def get_initial(self): state = get_object_or_404(mymodel, some_field=some_value) return { 'state':state, } hope helps!
Comments
Post a Comment