我有一个模型表单,我正在编写一个自定义窗口小部件,以便替换多对多窗体。使用jQuery FCBKcomplete窗口小部件选择多个字段。虽然替换multiselect元素工作正常,但它不再为多选项提供选项。
这是我的小部件:
class FCBKcompleteWidget(forms.SelectMultiple):
def _media(self):
return forms.Media(js=(reverse('appstatic',
args=['js/jquery.fcbkcomplete.min.js']),
reverse('appstatic',
args=['js/init-fcbkcomplete.js'])),
css={'all': (reverse('appstatic',
args=['css/jquery.fcbkcomplete'
'.css']),)})
media = property(_media)
这是我的表格:
class BlogForm(forms.ModelForm):
class Meta(object):
model = models.Blog
exclude = ('slug',)
def __init__(self, *args, **kwargs):
super(BlogForm, self).__init__(*args, **kwargs)
self.fields['description'].widget = TinyMCEWidget()
fcbkcomplete_fields = ['categories', 'admins', 'editors']
for field in fcbkcomplete_fields:
self.fields[field].widget = FCBKcompleteWidget()
以下是我的模特:
class Category(models.Model):
"""A blog category"""
title = models.CharField(max_length=128)
slug = models.SlugField()
class Meta(object):
verbose_name_plural = u'Categories'
def __unicode__(self):
return self.title
@models.permalink
def get_absolute_url(self):
return ('category', (), {'slug': self.slug})
class Blog(models.Model):
"""A blog"""
title = models.CharField(max_length=128)
slug = models.SlugField(unique=True)
description = models.TextField()
categories = models.ManyToManyField(Category, related_name='blogs')
shared = models.BooleanField()
admins = models.ManyToManyField(User, related_name='blog_admins')
editors = models.ManyToManyField(User, related_name='blog_editors')
def __unicode__(self):
return self.title
@models.permalink
def get_absolute_url(self):
return ('blog', (), {'slug': self.slug})
以下是生成的HTML:
<div class="field">
<label for="name">Categories</label>
<select multiple="multiple" name="categories" id="id_categories">
</select>
<div class="help-text">trimmed for readability</div>
</div>
<div class="field">
<label for="name">Admins</label>
<select multiple="multiple" name="admins" id="id_admins">
</select>
<div class="help-text">trimmed for readability</div>
</div>
<div class="field">
<label for="name">Editors</label>
<select multiple="multiple" name="editors" id="id_editors">
</select>
<div class="help-text">trimmed for readability</div>
</div>
正如您所看到的,没有任何选项可以将其添加到multiselect元素中。当我没有用我的自定义窗口替换窗口小部件时,这是生成的HTML:
<div class="field">
<label for="name">Categories</label>
<select multiple="multiple" name="categories" id="id_categories">
<option value="1" selected="selected">Good Stuff</option>
</select>
<div class="help-text">trimmed</div>
</div>
<div class="field">
<label for="name">Admins</label>
<select multiple="multiple" name="admins" id="id_admins">
<option value="2" selected="selected">username</option>
<option value="3">some username</option>
<option value="4">another username</option>
</select>
<div class="help-text">trimmed</div>
</div>
<div class="field">
<label for="name">Editors</label>
<select multiple="multiple" name="editors" id="id_editors">
<option value="2" selected="selected">username</option>
<option value="3">some username</option>
<option value="4">another username</option>
</select>
<div class="help-text">trimmed</div>
</div>
有没有人对于为什么这些选项没有通过小部件更换流程提出任何建议?任何帮助将不胜感激。
答案 0 :(得分:0)
此类行为的原因似乎缺少fcbk字段的CHOICES属性 只需将选项推送到表单
class EmailSubscriptionFilterForm(forms.ModelForm):
class Meta:
model = EmailSubscription
exclude = ('dsts',)
def loc_name(self, id):
return Location.objects.get(id = id).name
def __init__(self, *args, **kwargs):
super(EmailSubscriptionFilterForm, self).__init__(*args, **kwargs)
fcbkcomplete_fields = ['orgs']
for field in fcbkcomplete_fields:
self.fields[field].widget = MultiOriginSelect()
if args:
self.fields['orgs'].choices = ([(int(o), self.loc_name(int(o))) for o in args[0].getlist('orgs')] )
到init,它会将POST请求附带的所有选项添加到选择主体。