无法覆盖 ModelForm (Django) 中的 SelectMultiple 小部件
我有一个 ManyToManyField,我想将其作为 CheckboxSelectMultiple 小部件以表单形式呈现。为什么这些方法都不起作用? (请参阅下面的尝试#1、#2 和#3。)根据我在文档和 SO 中阅读的所有内容,至少其中一个应该有效。但我仍然有一个顽固的 SelectMultiple 小部件拒绝让步。
from django.forms.widgets import CheckboxSelectMultiple
from django.db import models
from django import forms
KEYWORD_CHOICES = (('sky', 'sky'),('wind','wind'),)
class Keyword(models.Model):
keyword = models.CharField(max_length=50)
def __unicode__(self):
return self.keyword
class Feedback(models.Model):
summary = models.CharField(max_length=200)
keys = models.ManyToManyField(Keyword, blank=True, null=True)
###################################
class FeedbackForm(forms.ModelForm):
# attempt 1
# based on
# http://docs.djangoproject.com/en/dev/ref/forms/widgets/#specifying-widgets
keys = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=KEYWORD_CHOICES)
class Meta:
model = Feedback
# attempt 2
# based on
# http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
widgets = {
'keys':CheckboxSelectMultiple(),
}
# attempt 3
# based on
# bitkickers.blogspot.com/2010/05/django-manytomanyfield-on-modelform-as.html
def __init__(self, *args, **kwargs):
super(FeedbackForm, self).__init__(*args, **kwargs)
self.fields["keys"].widget = CheckboxSelectMultiple()
self.fields["keys"].queryset = Keywords.objects.all()
我使用了一个固定装置来填充我的初始关键字实例。
我还尝试覆盖 help_text 但没有成功。
在我的 views.py 中,我使用的是表单集:
from app.models import *
from django.forms.models import modelformset_factory
def submit(request):
FeedbackFormSet = modelformset_factory(Feedback, extra=1)
#[...]
feedbackforms = FeedbackFormSet(prefix='feedback',queryset=Feedback.objects.none())
但我不知道这会产生什么影响。
我使用的是 Django 版本 1.2.1。
I have a ManyToManyField that I want to present in a form, as a CheckboxSelectMultiple widget. Why don't any of these methods work? (See Attempt #1, #2 and #3 below.) According to everything I've read in the docs and on SO, at least one of them should work. But I still have a stubborn SelectMultiple widget that refuses to budge.
from django.forms.widgets import CheckboxSelectMultiple
from django.db import models
from django import forms
KEYWORD_CHOICES = (('sky', 'sky'),('wind','wind'),)
class Keyword(models.Model):
keyword = models.CharField(max_length=50)
def __unicode__(self):
return self.keyword
class Feedback(models.Model):
summary = models.CharField(max_length=200)
keys = models.ManyToManyField(Keyword, blank=True, null=True)
###################################
class FeedbackForm(forms.ModelForm):
# attempt 1
# based on
# http://docs.djangoproject.com/en/dev/ref/forms/widgets/#specifying-widgets
keys = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=KEYWORD_CHOICES)
class Meta:
model = Feedback
# attempt 2
# based on
# http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
widgets = {
'keys':CheckboxSelectMultiple(),
}
# attempt 3
# based on
# bitkickers.blogspot.com/2010/05/django-manytomanyfield-on-modelform-as.html
def __init__(self, *args, **kwargs):
super(FeedbackForm, self).__init__(*args, **kwargs)
self.fields["keys"].widget = CheckboxSelectMultiple()
self.fields["keys"].queryset = Keywords.objects.all()
I used a fixture to populate my initial Keyword instances.
I've also tried to override the help_text without success.
In my views.py
I am using a formset:
from app.models import *
from django.forms.models import modelformset_factory
def submit(request):
FeedbackFormSet = modelformset_factory(Feedback, extra=1)
#[...]
feedbackforms = FeedbackFormSet(prefix='feedback',queryset=Feedback.objects.none())
But I don't see how that would make a difference.
I am using Django version 1.2.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须告诉
modelformset_factory
使用您自定义的FeedbackForm
:之后,您的三种方法之一应该可以工作(对于 Django 1.2,我更喜欢第二种方法)。
You have to tell the
modelformset_factory
to use your customizedFeedbackForm
:After that, one of your three approaches should work (I'd prefer the second one for Django 1.2).