无法覆盖 ModelForm (Django) 中的 SelectMultiple 小部件

发布于 2024-09-14 07:46:26 字数 2030 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2024-09-21 07:46:26

您必须告诉 modelformset_factory 使用您自定义的 FeedbackForm

FeedbackFormSet = modelformset_factory(Feedback, form=FeedbackForm, extra=1)

之后,您的三种方法之一应该可以工作(对于 Django 1.2,我更喜欢第二种方法)。

You have to tell the modelformset_factory to use your customized FeedbackForm:

FeedbackFormSet = modelformset_factory(Feedback, form=FeedbackForm, extra=1)

After that, one of your three approaches should work (I'd prefer the second one for Django 1.2).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文