问题描述
在添加日历(“ Calendar”型号的日历)的表单中,“组”字段中显示一个下拉菜单,其中包含所有“ CalendarGroups”。我希望此菜单不存在,并且我希望通过url参数(“ group_id”)传递的“ group” ID默认设置“ group”。我该怎么解决?
在models.py中:
class CalendarGroups(models.Model):
name = models.CharField(max_length = 155,blank=True,null=True,unique=True)
def __str__(self):
return str(self.name)
@property
def get_html_url(self):
url = reverse('',args=(self.id,))
return f'<a href="{url}"> {self.name} </a>'
class Calendar(models.Model):
name = models.CharField(max_length=200,unique=True)
#created_by
group = models.ForeignKey(CalendarGroups,on_delete = models.CASCADE,default='')
@property
def get_html_url(self):
url = reverse('cal:calendar_view',self.group))
return f'<a href="{url}"> {self.name} </a>'
在forms.py中:
class CalendarGroupsForm(ModelForm):
class Meta:
model = CalendarGroups
fields = ('name',)
def __init__(self,*args,**kwargs):
super(CalendarGroupsForm,self).__init__(*args,**kwargs)
class CalendarForm(ModelForm):
class Meta:
model = Calendar
fields = ('name','group')
def __init__(self,**kwargs):
super(CalendarForm,**kwargs)
在views.py中:
def group(request,group_id=None):
instance = CalendarGroups()
if group_id:
instance = get_object_or_404(CalendarGroups,pk=group_id)
else:
instance = CalendarGroups()
form = CalendarGroupsForm(request.POST or None,instance=instance)
if request.POST and form.is_valid():
form.save()
return HttpResponseRedirect(reverse('cal:home'))
return render(request,'cal/form.html',{'form': form})
def calendar(request,group_id=None):
instance = Calendar()
form = CalendarForm(request.POST or None,{'form': form})
在urls.py中:
url(r'^home/(?P<group_id>\d+)/calendar/new/$',views.calendar,name='calendar_new')
解决方法
咨询了documentation之后,我通过如下修改代码来解决了这个问题:
在views.py中:
<Select defaultValue="" onChange={[Function (anonymous)]} style={{...}} transitionName="slide-up" choiceTransitionName="" bordered={true}></Select>
<Select virtual={[undefined]} dropdownMatchSelectWidth={[undefined]} defaultValue="" onChange={[Function (anonymous)]} style={{...}} transitionName="slide-up" choiceTransitionName="" listHeight={256} listItemHeight={24} mode={[undefined]} prefixCls="ant-select" direction={[undefined]} inputIcon={[Function: mergedSuffixIcon]} menuItemSelectedIcon={{...}} removeIcon={{...}} clearIcon={{...}} notFoundContent={{...}} className="" getPopupContainer={[undefined]} dropdownClassName="">
<ForwardRef(Select) virtual={[undefined]} dropdownMatchSelectWidth={[undefined]} defaultValue="" onChange={[Function (anonymous)]} style={{...}} transitionName="slide-up" choiceTransitionName="" listHeight={256} listItemHeight={24} mode={[undefined]} prefixCls="ant-select" direction={[undefined]} inputIcon={[Function: mergedSuffixIcon]} menuItemSelectedIcon={{...}} removeIcon={{...}} clearIcon={{...}} notFoundContent={{...}} className="" getPopupContainer={[undefined]} dropdownClassName="">
<div className="ant-select ant-select-single ant-select-show-arrow" style={{...}} onMouseDown={[Function: onInternalMouseDown]} onKeyDown={[Function: onInternalKeyDown]} onKeyUp={[Function: onInternalKeyUp]} onFocus={[Function: onContainerFocus]} onBlur={[Function: onContainerBlur]}>
<SelectTrigger disabled={[undefined]} prefixCls="ant-select" visible={[undefined]} popupElement={{...}} containerWidth={{...}} animation={[undefined]} transitionName="slide-up" dropdownStyle={[undefined]} dropdownClassName="" direction={[undefined]} dropdownMatchSelectWidth={[undefined]} dropdownRender={[undefined]} dropdownAlign={[undefined]} getPopupContainer={[undefined]} empty={false} getTriggerDOMNode={[Function: getTriggerDOMNode]}>
在forms.py中:
expect(wrapper.find("select").length).toEqual(1)
expect(wrapper.find("option").length).toEqual(3)
,
您已经将所需的group_id密钥发送到 views.calendar 。 现在尝试将其添加到form.save
之前的表单中尝试在view.py中进行这样的修改
def calendar(request,group_id):
instance = Calendar()
form = CalendarForm(request.POST or None,instance=instance)
if request.POST and form.is_valid():
form.group=group_id
form.save()
return HttpResponseRedirect(reverse('cal:home'))
return render(request,'cal/form.html',{'form': form})
请注意,这可能会覆盖用户可以在CalendarForm上发布的内容,因此您应该考虑隐藏或使该文件 group_id 不可编辑, 而且这与我通常所做的略有不同,但应该可以 我还发布了一个适用于我的示例:
def calendar(request,group_id):
instance = Calendar()
form = CalendarForm(request.POST or None)
if request.POST and form.is_valid():
instance = form.save(commit=False)
instance.group = group_id
instance.save()
return HttpResponseRedirect(reverse('cal:home'))
return render(request,{'form': form})