标签:none amp model bsp values ons nbsp render end
1. 添加沟通记录的两个需求
1.1 customer字段里面选的客户只能是我私户里面的客户
为form表单设置指定的choices
沟通记录的form class ConsultRecordForm(BootstrapBaseForm): class Meta: model = ConsultRecord exclude = [‘delete_status‘, ] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # 把customer字段的choice设置成我的客户 print(self.instance, id(self.instance)) print(‘&‘ * 120) # 方法一:修改字段的choices选项 # self.fields[‘customer‘].choices = Customer.objects.filter(consultant=self.instance.consultant).values_list(‘id‘,‘name‘) # 方法二 将form表的字段直接修改 self.fields[‘customer‘] = forms.models.ModelChoiceField(queryset=Customer.objects.filter(consultant=self.instance.consultant)) self.fields[‘customer‘].widget.attrs.update({‘class‘: ‘form-control‘})
1.2 跟进人默认是自己
form表单的字段设定默认值
# 添加和编辑沟通记录 def consult_record(request, edit_id=None): record_obj = ConsultRecord.objects.filter(id=edit_id).first() # None if not record_obj: record_obj = ConsultRecord(consultant=request.user) # 生成一个销售是我的ConsultRecord对象 print(record_obj, id(record_obj)) print(‘@‘ * 120) form_obj = ConsultRecordForm(instance=record_obj, initial={‘consultant‘: request.user}) return render(request, ‘consult_record.html‘, {‘form_obj‘: form_obj, ‘edit_id‘: edit_id})
标签:none amp model bsp values ons nbsp render end
原文地址:https://www.cnblogs.com/cavalier-chen/p/10009046.html