在这个问题之外,我有一个 Action 模型,它有一个外键,它指定一个动作重复发生的频率:
class Reoccurance(models.Model):
label = models.CharField("Label", max_length=50, unique = True)
days = models.IntegerField("Days")
def __unicode__(self):
return self.label
class Meta:
ordering = ['days']
class Action(models.Model):
name = models.CharField("Action Name", max_length=200, unique = True)
complete = models.BooleanField(default=False, verbose_name="Complete?")
reoccurance = models.ForeignKey(Reoccurance, blank=True, null=True, verbose_name="Reoccurance")
我正在制作一个导致重复出现的 HTML 代码的 action 的 modelForm(基于重复出现表中存在的数据库值):
<select id="id_reoccurance" class="selectwithtitles" name="reoccurance">
<option value="" title="" >---------</option>
<option value="12" title="2" >2 Days</option>
<option value="1" title="3" >3 Days</option>
<option value="2" title="5" >5 Days</option>
<option value="10" title="6" >6 Days</option>
<option value="9" title="7" >1 Week</option>
<option value="3" title="10" >10 Days</option>
<option value="4" title="14" >2 Weeks</option>
<option value="11" title="21" >3 Weeks</option>
<option value="5" title="30" >1 Month</option>
<option value="13" title="42" >6 Weeks</option>
<option value="6" title="90" >1 Quarter</option>
<option value="7" title="180" >6 Months</option>
<option value="8" title="365" >1 Year</option>
</select>
我通过子类字段生成标题:
from django.utils.html import conditional_escape, escape
from django.utils.encoding import force_unicode
class SelectWithTitles(forms.Select):
def __init__(self, *args, **kwargs):
super(SelectWithTitles, self).__init__(*args, **kwargs)
# Ensure the titles dict exists
self.titles = {}
def render_option(self, selected_choices, option_value, option_label):
title_html = (option_label in self.titles) and \
u' title="%s" ' % escape(force_unicode(self.titles[option_label])) or ''
option_value = force_unicode(option_value)
selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
return u'<option value="%s"%s%s>%s</option>' % (
escape(option_value), title_html, selected_html,
conditional_escape(force_unicode(option_label)))
class ChoiceFieldWithTitles(forms.ChoiceField):
widget = SelectWithTitles
def __init__(self, choices=(), *args, **kwargs):
choice_pairs = [(c[0], c[1]) for c in choices]
super(ChoiceFieldWithTitles, self).__init__(choices=choice_pairs, *args, **kwargs)
self.widget.titles = dict([(c[1], c[2]) for c in choices])
def clean(self, value):
self.required = False
if not value and not self.required:
return value
return super(ChoiceFieldWithTitles, self).clean(value)
class ActionForm(forms.ModelForm):
reoccurance = ChoiceFieldWithTitles()
def __init__(self, *args, **kwargs):
super(ActionForm, self).__init__(*args, **kwargs)
choices = []
for pt in Reoccurance.objects.all():
choices.append((pt.id, pt.label, pt.days))
self.fields['reoccurance'] = ChoiceFieldWithTitles(choices = choices)
def clean(self):
cleaned_data = super(ActionForm, self).clean()
if cleaned_data.get("reoccurance") != '':
rec = Reoccurance.objects.get(pk=cleaned_data.get("reoccurance"))
self.cleaned_data['reoccurance'] = rec
return super(ActionForm, self).clean()
我必须将 clean 方法添加到 ActionForm 因为我必须返回数据库 Reoccurance 对象,而不仅仅是它的 id。
我还必须向 ChoiceFieldWithTitles 添加一个干净的方法,因为我无法让它允许重复出现一个空白值。即使我使用 reoccurance = ChoiceFieldWithTitles(required=False),它也视需要而定。
我现在的问题是我似乎不能允许空白值。如果用户选择下拉菜单的第一个选项:
<option value="" title="" >---------</option>
表单返回此错误:
ValueError at /next_actions/test-action-9712,778/edit/
Cannot assign "u''": "Action.reoccurance" must be a "Reoccurance" instance.
我需要对 ActionForm 的 clean 方法执行什么操作以允许重复字段为空值?