我正在开发一个Python / Django项目,表单上的一个字段目前已被破坏(它是datetimepicker
字段 - 问题是您无法选择日期以外的日期2017年1月1日)。这似乎是因为不再支持用于添加datetimepicker
的库。)
我将考虑自己实施datetimepicker
,但需要允许用户选择此表格上的01/01/2017之后的日期 - 以便保存预定会议的日期和时间等。
我现在的想法是通过在日期/时间的表单中添加简单的EasyText
字段来创建临时修复。我知道这意味着输入的数据将不会被验证,因此存在用户输入错误的空间,但它将使用户能够至少临时设置会议的日期/时间,直到我解决当前的问题{ {1}}字段。
我想要添加这些字段的表单在forms.py中定义为:
datetimepicker
我已将class PostDepMeetingForm(ValidatedForm):
postdep_meeting_date = MoonDateTimeField(required=False, widget=forms.DateTimeInput(format='%d/%m/%Y %H:%M', attrs=({'class':'datetimepicker'})))
planning_who = forms.CharField(required=False)
general_notes = EasyText(label='Survey notes')
#ERF(16/12/2016 @ 1345) Add a text area for the meeting date & time
meeting_date_time = EasyText(label='Meeting date & time')
class Meta:
model = Survey
fields = ('postdep_meeting_date', 'planning_who','building_regs','general_notes', 'meeting_date_time')
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance', {})
project = instance.project
try:
postdep_meeting = project.meetings.get(purpose=3)
self.postdep_meeting_id = postdep_meeting.id
self.postdep_meeting_creator = postdep_meeting.event_creator or ''
postdep_meeting_date = postdep_meeting.date
general_notes = postdep_meeting.notes
#ERF(16/12/2016 @ 1420) Try setting the meeting_date_time EasyText's default value to postdep_meeting.date & postdep_meeting.time
#meeting_date_time
except ObjectDoesNotExist:
postdep_meeting_date = None
general_notes = None
try: planning_who = project.assigned.select_related('employee').get(role=Role.PL).employee.id
except ObjectDoesNotExist: planning_who = None
initial = kwargs.get('initial', {})
initial={
'postdep_meeting_date': postdep_meeting_date,
'general_notes': general_notes,
'planning_who': planning_who,
}
kwargs['initial'] = initial
super(PostDepMeetingForm, self).__init__(*args, **kwargs)
self.fields['planning_who'] = AutoFlexiSelect(model='e', choices=get_choices('ARCHITECT_CHOICES') + [('*', 'Other')], label="Who", current_id=planning_who,)
self.fields['postdep_meeting_date'].widget.attrs.update({'data-meeting-id': getattr(self,'postdep_meeting_id', ''), 'data-meeting-creator': getattr(self,'postdep_meeting_id', '')})
def save(self, commit=True):
project = self.instance.project
data = self.cleaned_data
if 'postdep_meeting_date' in self.changed_data:
postdep_meeting = Meeting.objects.get_or_create(project=project, purpose=3)[0]
postdep_meeting.date = data['postdep_meeting_date']
postdep_meeting.notes = data['general_notes']
postdep_meeting.save()
if data['planning_who']:
try: project.assigned.select_related('employee').get(role=Role.PL).delete()
except ObjectDoesNotExist: pass
pe = ProjectEmployee.objects.create(role=Role.PL, project=project, employee_id=data['planning_who'])
return super(PostDepMeetingForm, self).save(commit=commit)
字段EasyText
添加到表单中,当我在浏览器中查看该页面时,可以看到它显示在表单上。
我不确定现在该怎么做 - 保存用户输入的任何输入,并在下次浏览该页面时自动将其输入到该字段中的任何内容显示...我会吗?需要向meeting_date_time
模型添加新字段以保存此信息吗?
修改
根据评论中的要求,project
定义为:
ValidatedForm
和class ValidatedForm(forms.ModelForm):
error_css_class = 'error-field'
required_css_class = 'required'
def __init__(self, *args, **kwargs):
super(ValidatedForm, self).__init__(*args, **kwargs)
# for k, field in self.fields.items():
# if 'required' in field.error_messages:
# # print 'field error', field, field
# # field.widget.attrs['class'] = 'error-field'
# print 'field error', k
# field.error_messages['required'] = '*'
# else:
# print 'Field erroR', field
是一个'文本区域'用于在表单上存储用户输入,用以下内容定义:
EasyText