选框的manytomany字段在django模板中设置了选择字段

选框的manytomany字段在django模板中设置了选择字段

本文介绍了具有复选框的manytomany字段在django模板中设置了选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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) b $ b return unicode(self.id)

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

  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()
...


解决方案

要显示一个 ManyToManyField 作为复选框而不是选择字段,您需要在 Meta 类适合的 ModelForm 子类。然后在每个复选框上显示一个自定义标签,。您还可以将HTML标签添加到 label_from_instance 返回的字符串中,使其看起来像您想要的那样漂亮,但请记住将返回的字符串换成。

 从django.forms.widgets导入CheckboxSelectMultiple 
from django.forms.models import ModelMultipleChoiceField
...
class CustomSelectMultiple(ModelMultipleChoiceField):
def label_from_instance(self,obj):
返回%s:%s%s%(obj.name,obj.short_description,obj.price)

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


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()
    ...
解决方案

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(),}

这篇关于具有复选框的manytomany字段在django模板中设置了选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:14