django multivaluefield& multiwidget - 压缩和/或解压缩不工作 [英] django multivaluefield & multiwidget - compress and/or decompress not working

查看:163
本文介绍了django multivaluefield& multiwidget - 压缩和/或解压缩不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部



我有一个带有几个的MultiValueField的表单。它使用一个选择字段和字符(与对应的选择和TextInput的小部件)::

  custom_choices = [(one ,one),(two,two),(other,other)] 

class MyMultiWidget(forms.MultiWidget):
def __init __ ,* args,** kwargs):
widgets =(
forms.Select(choices = custom_choices),
forms.TextInput(),

super MyMultiWidget,self).__ init __(widgets,* args,** kwargs)

def解压缩(self,value):
如果值:
return value.split( )
return ['','']

class MyMultiValueField(forms.MultiValueField):
def __init __(self,* args,** kwargs):
fields =(
forms.ChoiceField(required = True),
forms.CharField(max_length = 128,required = False),

super(MyMultiValueField,self)。 __init __(fields,* args,** kwargs)
self.widget = T如果data_list为
,则返回'|'.join(data_list)

class MyTestField(
def formfield(self,** kwargs):
return super(MyTestField,self).formfield(form_class = MyMultiValueField)

class MyModel(models。模型):
myField = MyTestField()

压缩函数似乎工作;它按预期返回两个字符串的列表。但解压缩中的值参数始终为无。果然,当我直接检查数据库时,myField列始终设置为null。压缩和解压缩之间发生什么样的想法?为什么没有实际存储压缩的值?



谢谢。

解决方案>

您应该从实际包含数据的某个字段继承MyTestField。在你的情况下,它可以是CharField或TextField。我认为会解决问题。


All,

I have a form with a MultiValueField that almost works. It uses a choicefield and charfield (with a corresponding Select and TextInput for the widgets)::

custom_choices = [("one","one"),("two","two"),("other","other")]

class MyMultiWidget(forms.MultiWidget):
    def __init__(self,*args,**kwargs):
        widgets = (
            forms.Select(choices=custom_choices),
            forms.TextInput(),
        )
        super(MyMultiWidget, self).__init__(widgets,*args,**kwargs)

    def decompress(self, value):
        if value:
            return value.split("|")
        return ['', '']

class MyMultiValueField(forms.MultiValueField):
    def __init__(self, *args, **kwargs):
        fields = (
            forms.ChoiceField(required=True),
            forms.CharField(max_length=128,required=False),
        )
        super(MyMultiValueField, self).__init__(fields, *args, **kwargs)
        self.widget = TestMultiWidget()

    def compress(self, data_list):
        if data_list:
            return '|'.join(data_list)

class MyTestField(models.Field):
    def formfield(self, **kwargs):
        return super(MyTestField, self).formfield(form_class=MyMultiValueField)

class MyModel(models.Model):
    myField = MyTestField()

The compress function seems to be working; it returns a list of two strings as expected. But the "value" argument in decompress is always None. Sure enough, when I check the database directly, the myField column is consistently set to null. Any ideas what's happening inbetween compress and decompress? Why isn't the value from compress actually being stored?

Thanks.

解决方案

You should inherit MyTestField from some field which actually contain the data. In your case it can be CharField or TextField. I think it will solve the problem.

这篇关于django multivaluefield& multiwidget - 压缩和/或解压缩不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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