带有复选框的表单中的多域字段在 django 模板中插入了选择字段

发布于 2024-12-22 14:59:48 字数 774 浏览 0 评论 0 原文

我有 ManyToManyField 模型。现在我需要表单,但不需要模板中的选择字段。

class Foo(models.Model):
    name = models.CharField(max_length=50)
    short_description = models.CharField(max_length=100)
    price = models.IntegerField()

    def __unicode__(self):
        return self.name

class Bar(models.Model):
    foo = models.ManyToManyField(Foo, blank=True, null=True, related_name='foos')

    def __unicode__(self):
        return unicode(self.id)

我真正需要的是在模板中使用复选框显示 Foo 模型中的所有数据,而不是在模板中使用 model.Form 和 {{ form }} 调用时选择我拥有的字段。

class BarForm(forms.ModelForm):
    class Meta:
        model = Bar

视图.py

def show_form(request, id):
    foo = get_object_or_404(Foo, id=id)
    form = BarForm()
    ...

I have model with ManyToManyField. Now I need form but don't need select field in template.

class Foo(models.Model):
    name = models.CharField(max_length=50)
    short_description = models.CharField(max_length=100)
    price = models.IntegerField()

    def __unicode__(self):
        return self.name

class Bar(models.Model):
    foo = models.ManyToManyField(Foo, blank=True, null=True, related_name='foos')

    def __unicode__(self):
        return unicode(self.id)

What I really need is to display all data from the Foo model with checkboxes in template, instead of select field which I have if use model.Form and {{ form }} call in template.

class BarForm(forms.ModelForm):
    class Meta:
        model = Bar

view.py

def show_form(request, id):
    foo = get_object_or_404(Foo, id=id)
    form = BarForm()
    ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

很快妥协 2024-12-29 14:59:48

要将 ManyToManyField 显示为复选框而不是选择字段,您需要在相应 ModelForm 子类的 Meta 类中设置小部件。然后,要在每个复选框上显示自定义标签,创建您自己的表单字段类派生自 ModelMultipleChoiceField,并覆盖 label_from_instance。您还可以将 HTML 标签添加到 label_from_instance 返回的字符串中,使其看起来像您想要的那样漂亮,但请记住用 mark_safe

from django.forms.widgets import CheckboxSelectMultiple
from django.forms.models import ModelMultipleChoiceField
...
class CustomSelectMultiple(ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return "%s: %s %s" %(obj.name, obj.short_description, obj.price)

class BarForm(forms.ModelForm):
    foo = CustomSelectMultiple(queryset=Foo.objects.all())
    class Meta:
        model = Bar
        widgets = {"foo":CheckboxSelectMultiple(),}

To show a ManyToManyField as checkboxes instead of a select field, you need to set the widget in the Meta class of the appropriate ModelForm subclass. Then to show a custom label on each checkbox, create your own form field class derived from ModelMultipleChoiceField, and override label_from_instance. You may also add HTML tags to the string returned by label_from_instance to make it look as pretty as you want, but remember to wrap the returned string with mark_safe.

from django.forms.widgets import CheckboxSelectMultiple
from django.forms.models import ModelMultipleChoiceField
...
class CustomSelectMultiple(ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return "%s: %s %s" %(obj.name, obj.short_description, obj.price)

class BarForm(forms.ModelForm):
    foo = CustomSelectMultiple(queryset=Foo.objects.all())
    class Meta:
        model = Bar
        widgets = {"foo":CheckboxSelectMultiple(),}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文