Django通用创建视图模型选择字段的默认值
我有一个从通用CreateView继承的视图,并覆盖get_initial方法,如下所示:Django通用创建视图模型选择字段的默认值,django,django-forms,django-generic-views,Django,Django Forms,Django Generic Views,我有一个从通用CreateView继承的视图,并覆盖get_initial方法,如下所示: class PosterVisualCreateView (ModelFormMixin, generic.edit.CreateView, ObjectClassContextMixin): model = Poster valid_message = "Successfully created object." template_name = "poser/create_pos
class PosterVisualCreateView (ModelFormMixin, generic.edit.CreateView, ObjectClassContextMixin):
model = Poster
valid_message = "Successfully created object."
template_name = "poser/create_poster_visual.html"
def get_form_class (self):
return super(PosterVisualCreateView, self).get_form_class(extra="CreateVisual")
def get_form_kwargs (self):
kwargs = super(PosterVisualCreateView, self).get_form_kwargs()
kwargs.update({
"company": self.request.company
})
return kwargs
def get_context_data (self, **kwargs):
context = super(PosterVisualCreateView, self).get_context_data(**kwargs)
context.update({
"company": self.request.company,
})
return context
def get_initial (self):
initial = super(PosterVisualCreateView, self).get_initial()
initial.update({
"company": self.request.company,
"template": self.request.company.template_set.all().first()
})
return initial
def form_valid(self, form):
success_url = super(PosterVisualCreateView, self).form_valid(form)
attributes = form.instance.create_attributes()
for attribute in attributes:
attribute.poster = form.instance
attribute.save()
form.instance.save()
form.instance.render_html(commit=True)
form.instance.save()
return success_url
@method_decorator(login_required)
def dispatch (self, *args, **kwargs):
return super(PosterVisualCreateView, self).dispatch(*args, **kwargs)
该页面显示此表单:
class PosterFormCreateVisual (CompanyHiddenForm):
"""Create form for Posters."""
template = fields.ModelChoiceField(widget=forms.RadioSelect, queryset=Template.objects.all())
product = fields.ModelChoiceField(widget=forms.Select, queryset=Product.objects.all(),
required=False)
class Meta:
model = Poster
fields = ("template", "product", "company")
def __init__ (self, *args, **kwargs):
company = kwargs.pop("company", None)
assert company is not None, "Company is required to create attribute form."
super(PosterFormCreateVisual, self).__init__(*args, **kwargs)
self.fields["template"].queryset = company.template_set.all()
self.fields["product"].queryset = company.product_set.all()
初始值应该是模板无线电选择中的第一项,但事实并非如此,有人能帮我吗?试试这个:
"template": self.request.company.template_set.all().first()
但据我所知,您的原始代码也应该工作
顺便问一下,你是如何测试表格的?通过点击“刷新”按钮或
Ctrl-R
/F5
键?某些浏览器重新加载页面,但保存以前选择/输入的表单值。要检查初始值,应通过单击地址栏(或按Ctrl-L
),然后按Enter
键来重新加载表单页面。尝试“模板”:template.objects.all()[0]
在get_initial
中,并非所有模板都属于当前公司,因此不要将系统中所有模板中的第一个模板选为默认模板,因为它可能不属于用户。我明白了。您是否尝试过在没有“.id”的情况下仅将其设置为模型对象?我已经有一段时间没有使用get_initial了,但我认为您可能不想使用id。同样在您当前的代码中,公司的首字母是否正确地显示在表单中?公司隐藏在表单中,因为它不可选择,这取决于您当前的登录名。删除.id没有什么区别。我用一个示例模型/表单对它进行了测试,效果很好。请用视图和模板的完整代码更新问题。嗯,我甚至不能运行此代码ModelFormMixin
与CreateView
冲突(无法创建一致的方法解析顺序(MRO))。而get\u form\u class()
没有像extra
这样的参数。很抱歉,这在这里没有帮助。extra
参数确保获取的表单类是PosterFormCreateVisual,即“PosterForm”+“CreateVisual”
,而不是默认的PosterForm。