型号:
class MyModel(model.Models)
status = models.ForeignKey(Statuses, on_delete=models.PROTECT, null=True, blank=True)
观点:
class MyViewCreate(CreateView):
model = MyModel
form_class = MyFrom
template_name = 'mymodel_form.html'
形式:
class MyForm(ModelForm):
class Meta:
model = MyModel
fields = '__all__'
我如何在选项列表中设置默认值或第一个值>
答案 0 :(得分:5)
您可以像这样使用initial
:
class MyViewCreate(CreateView):
model = MyModel
form_class = MyFrom
initial = {'status': '0'}
template_name = 'mymodel_form.html'
或者像这样:
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,
}
希望它有所帮助!