Django Checkbox从ModelForm中选择多项覆盖'选择' [英] Django CheckboxSelectMultiple override 'choices' from ModelForm

查看:364
本文介绍了Django Checkbox从ModelForm中选择多项覆盖'选择'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够以django形式提取不同的信息:

I would like to be able to extract different information in my django form:

这是我的形式:

<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

class InstanceForm(ModelForm):
    class Meta:
        model = models.BaseAsset
        widgets = {
            'labels': LabelIconCheckboxSelectMultiple()
        }

模型:

class AssetClass(models.Model):
    default_labels = models.ManyToManyField(Label, null=True, blank=True)
    pass

M2M参考字段

class Label(models.Model):
    explanation = models.CharField(null=True, max_length=63)
    svgpreview  = models.CharField(null=True, max_length=31)
    def __unicode__(self):
        return unicode(self.explanation)
    pass

{{form.as_p}} 生成的HTML代码如下:

Now, the HTML code generated by the {{ form.as_p }} is as follows:

<li><label for="id_labels_0"><input type="checkbox" name="labels" value="1" id="id_labels_0" /> Consult owner before using</label></li>
<li><label for="id_labels_1"><input type="checkbox" name="labels" value="2" id="id_labels_1" /> This item is broken</label></li>

这意味着它显然使用 __ unicode __ 的模型标签。如何在选择窗口小部件中更改该行为,以便使用其他函数来填充它的选择?我试图以相当便携的方式来获取它,以打印'< img src ={{label.svgpreview}}alt ={{label.explanation}}..

Which means it's clearly using the __unicode__ rendering of the model 'Label'. How can I change that behavior in the Select widget, so that it would use a different function to populate it's choices? I'm trying to get it, in the reasonably portable way, to print '<img src="{{label.svgpreview}}" alt="{{label.explanation}}"...>' next to the checkbox?

推荐答案

阅读 django。 form.models.ModelChoiceField 给出一个提示:

# this method will be used to create object labels by the QuerySetIterator.
# Override it to customize the label.
def label_from_instance(self, obj):
    """
    This method is used to convert objects into strings; it's used to
    generate the labels for the choices presented by this object. Subclasses
    can override this method to customize the display of the choices.
    """
    return smart_unicode(obj)

好的,但是如何覆盖 ModelForm 的每个实例 - 这在整个 django.forms

ok, but how do I override it per-instance of ModelForm - this gets overridden in few places throughout django.forms

考虑到以下代码:

class InstanceForm(ModelForm):
    class Meta:
        model = models.BaseAsset
        widgets = {
            'labels': forms.CheckboxSelectMultiple()
        }


    def __init__(self, *args, **kwargs):
        def new_label_from_instance(self, obj):
            return obj.svgpreview

        super(InstanceForm, self).__init__(*args, **kwargs)
        funcType = type(self.fields['labels'].label_from_instance)
        self.fields['labels'].label_from_instance = funcType(new_label_from_instance, self.fields['labels'], forms.models.ModelMultipleChoiceField)



< $ b 覆盖实例级别的方法

请阅读引用线程中的注释,了解为什么这可能是一个坏主意。

Please read the comments in the referenced thread to understand why this might be a bad idea in general..

这篇关于Django Checkbox从ModelForm中选择多项覆盖'选择'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆