在django表单中自定义窗口小部件返回列表作为值而不是字符串 [英] In django forms custom widget return list as value instead of string

查看:168
本文介绍了在django表单中自定义窗口小部件返回列表作为值而不是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个自定义的窗口小部件,我想返回一个列表作为值。从我可以找到设置返回的值创建一个自定义的value_from_datadict函数。我已经做了这个

  def value_from_datadict(self,data,files,name):
value = data.get名称,无)
如果值:
#拆分sting,以便我们有一个节点列表
tag_list = value.strip(',')。split(',')
retVal = []
#循环通过节点
在tag_list中的节点:$ b​​ $ b#node string应该是以下形式:node_id-uuid
strVal = str(node) .split( - )

uuid = strVal [-1]
node_id = strVal [0]

#为节点创建一个node_id和uuid的元组
retVal.append({'id':node_id,'uuid':uuid})

如果retVal:
#如果retVal不为空。即我们有一个节点列表
#return this。如果它是空的,那么只需返回任何数据
return retVal

返回值

我希望这可以返回一个列表,但是当我打印出作为字符串而不是列表返回的值时。字符串本身包含正确的文本,但是我说它是一个字符串而不是一个列表。返回的例子可以是

  [{'id':'1625','uuid':None}] 

但是如果我做了str [0],它将打印出来,而不是{'id':'1625 ','uuid':无}



如何阻止它将我的列表转换成字符串?



谢谢

解决方案

嗯,很简单:如果你有一个 CharField ,那么你会得到一个字符串,因为 CharField 使用方法 to_python ,强制结果到字符串。您需要为此创建您自己的字段并返回列表。



OLD



您可以发布以下结果:

  x = value_from_datadict (..)
打印类型(x)

所以我们可以看到,究竟是返回?



您可以发布您正在使用的整个测试用例来提供示例吗?


I am writting a custom widget which I want to return a list as the value. From what I can find to set the value that is returned you create a custom value_from_datadict function. I have done this

def value_from_datadict(self, data, files, name):
    value = data.get(name, None)
    if value:
        # split the sting up so that we have a list of nodes
        tag_list = value.strip(',').split(',')
        retVal = []
        # loop through nodes
        for node in tag_list:
            # node string should be in the form: node_id-uuid
            strVal = str(node).split("-")

            uuid = strVal[-1]
            node_id = strVal[0]

            # create a tuple of node_id and uuid for node
            retVal.append({'id': node_id, 'uuid': uuid})

        if retVal:
            # if retVal is not empty. i.e. we have a list of nodes
            # return this. if it is empty then just return whatever is in data
            return retVal

    return value

I expect this to return a list but when I print out the value it is returned as a string rather than a list. The string itself contains the right text but as i said it is a string and not a list. An example of what is returned could be

[{'id': '1625', 'uuid': None}]

but if I did str[0] it would print out [ instead of {'id': '1625', 'uuid': None}

How can I stop it from converting my list into a string?

Thanks

解决方案

Well, it's simple: if you have a CharField, then you will get a string as a result, because CharField uses method to_python, that coerces the result to string. You need to create your own Field for this one and return a list.

OLD

Could you post the result of:

x = value_from_datadict(..)
print type(x)

so we can see, what exactly is returned?

And could you post the whole test case you are using to deliver the example?

这篇关于在django表单中自定义窗口小部件返回列表作为值而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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