gpt4 book ai didi

Django 。如何设置默认选项使用通用 CreateView

转载 作者:行者123 更新时间:2023-12-01 11:26:10 29 4
gpt4 key购买 nike

型号:

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__'

我如何在表单中设置默认值或选项列表中的第一个值>

最佳答案

你可以像这样使用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,
}

希望对您有所帮助!

关于 Django 。如何设置默认选项使用通用 CreateView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45692129/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com