我有一个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")
我正在制作一个modelForm of Action,它导致HTML代码重新出现(基于Reoccurance表存在的数据库值):
<select id="id_reoccurance" class="select" name="reoccurance"> <option value="" selected="selected">---------</option> <option value="12">2 Days</option> <option value="1">3 Days</option> <option value="2">5 Days</option> <option value="10">6 Days</option> <option value="9">1 Week</option> <option value="3">10 Days</option> <option value="4">2 Weeks</option> <option value="11">3 Weeks</option> <option value="5">1 Month</option> <option value="13">6 Weeks</option> <option value="6">1 Quarter</option> <option value="7">6 Months</option> <option value="8">1 Year</option> </select>
正如您所看到的,虽然选择按升序排序,但值是乱序的,因为它们是无序输入数据库的.
我想创建一些动态计算动作重现的日期的jquery.它将需要今天的日期并添加与用户选择的选择相对应的天数.但是根据我在表单中获得的数据,我无法将选择转换为设定的天数.即使选项的值是有序的,它仍然不表示说“1年”等于365天.
此数据虽然在Reoccurance表中.对于label =“1 Year”,days = 365.同样,Reoccurance表中的所有项目.
有没有办法重写我的modelForm,以便每个下拉项的选项值可能等于该选择的天数?因为那时,我可以访问天数:
$("#id_reoccurance").change(function() { alert( $(this).val() ); });
这会不会影响我将正确的重复选择绑定到Reoccurance表的正确行的能力?有没有其他方法可以在我的表单模板上访问jquery中的这几天/标签领带?
更新
感谢Joseph建议检查this post,我能够在我的选项元素中包含一个标题:
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=(),**kwargs): choice_pairs = [(c[0],c[1]) for c in choices] super(ChoiceFieldWithTitles,self).__init__(choices=choice_pairs,**kwargs) self.widget.titles = dict([(c[1],c[2]) for c in choices]) class ActionForm(forms.ModelForm): reoccurance = ChoiceFieldWithTitles() def __init__(self,**kwargs): super(ActionForm,**kwargs) choices = [] for pt in Reoccurance.objects.all(): choices.append((pt.id,pt.label,pt.days)) self.fields['reoccurance'] = ChoiceFieldWithTitles(choices = choices)
太棒了.所以现在我在模板中得到以下内容:
<select id="id_reoccurance" class="selectwithtitles" name="reoccurance"> <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>
好吧,好像我们几乎就在那里,但是我被绊倒了.在jquery中,我正在尝试以下方法来获取所选项的标题:
$(function() { $("#id_reoccurance").change(function() { alert($(this).attr('title')); }); });
问题是它回归未定义!
更新了
$(function() { $("#id_reoccurance").change(function() { alert($(this).find("option:selected").attr("title")); }); });
解决方法
基于
this question/answer(这是粗略的,没有经过测试,希望是一个开始):
class myClassForm(forms.Form): class Meta: model = myClass fields=["name"] reoccurrance = forms.ChoiceField(label="Reoccurance",widget=forms.Select(attrs={'class':'selector'})) def __init__(self,**kwargs): super(myClassForm,**kwargs) choices = [] for pt in Reoccurance.objects.all(): choices.append((pt.id,unicode(pt.days))) self.fields['reoccurrance'].choices = choices
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。