获取django表单下拉列表选择中的其他数据

我有一个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代码重新出现(基于Reoccurance表存在的数据库值):

  --------- 2 Days 3 Days 5 Days 6 Days 1 Week 10 Days 2 Weeks 3 Weeks 1 Month 6 Weeks 1 Quarter 6 Months 1 Year  

正如您所看到的,虽然选择按升序排序,但值不按顺序,因为它们是按顺序输入数据库的。

我想创建一些动态计算动作将重现的日期的jquery。 它将采用今天的日期并添加与用户选择的选择相对应的天数。 但是根据我在表单中获得的数据,我无法将选择转换为设定的天数。 即使选项的值是有序的,它仍然不表示说“1年”等于365天。

这个数据虽然在Reoccurance表中。 对于label =“1 Year”,days = 365.同样,Reoccurance表中的所有项目。

有没有办法重写我的modelForm,以便每个下拉项的选项值可能等于该选择的天数? 因为那时,我可以访问天数:

 $("#id_reoccurance").change(function() { alert( $(this).val() ); }); 

这会不会影响我将正确的重复选择绑定到Reoccurance表的正确行的能力? 有没有其他方法可以在我的表单模板上访问jquery中的这些日期/标签领带?

更新

感谢Joseph建议查看这篇文章 ,我能够在我的选项元素中包含一个标题:

 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'%s' % ( 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]) 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) 

太棒了。 所以现在我在我的模板中得到以下内容:

  2 Days 3 Days 5 Days 6 Days 1 Week 10 Days 2 Weeks 3 Weeks 1 Month 6 Weeks 1 Quarter 6 Months 1 Year  

好吧,好像我们几乎就在那里,但是我被绊倒了。 在jquery中,我正在尝试以下方法来获取所选选项的标题:

 $(function() { $("#id_reoccurance").change(function() { alert($(this).attr('title')); }); }); 

问题是它回归未定义!

更新

 $(function() { $("#id_reoccurance").change(function() { alert($(this).find("option:selected").attr("title")); }); }); 

基于这个问题/答案 (这是粗略的,没有经过测试,希望是一个开始):

 class myClassForm(forms.Form): class Meta: model = myClass fields=["name"] reoccurrance = forms.ChoiceField(label="Reoccurance", choices=(), widget=forms.Select(attrs={'class':'selector'})) def __init__(self, *args, **kwargs): super(myClassForm, self).__init__(*args, **kwargs) choices = [] for pt in Reoccurance.objects.all(): choices.append((pt.id, unicode(pt.days))) self.fields['reoccurrance'].choices = choices