简体   繁体   English

清单不是清单时的Python吗? 列表对象没有属性拆分

[英]Python when a list isn't a list? list object has no attribute split

I have a list of user ids being sent to my API: 我有一个发送到我的API的用户ID列表:

Users = ['x1','x2']

In my serializer create method I try to iterate over them: 在我的序列化器create方法中,我尝试遍历它们:

    users = validated_data.get('users', None)
    for user in users:
        print(user)
        print("===")

The output I receive is: 我收到的输出是:

> x1,x2
> ===

It instead of iterating over the list, it outputs as one line! 它而不是遍历列表,而是输出为一行! This suggests to me it is not a list, but a string, so I checked the type(users) which gave a blank ie no type. 这对我来说不是一个列表,而是一个字符串,所以我检查了提供空白的type(users) ,即没有类型。

So I tried splitting users up users.split() Then I get a contradiction" " list object has no attribute split "! 因此,我尝试将用户拆分为users.split()然后出现矛盾“ 列表对象没有属性拆分 ”!

What is wrong here, confused? 这里出什么问题了,感到困惑吗?

It seems that users is a list that looks like this: ['x1,x2'] . 看来用户是一个看起来像这样的列表: ['x1,x2'] Instead of what you expected: ['x1','x2'] 而不是您期望的: ['x1','x2']

You can use ast.literal_eval to make a list first, or you can just split that one element: 您可以使用ast.literal_eval首先创建列表,也可以仅拆分一个元素:

users = ['x1,x2']
for user in users[0].split(','):
    print(user)
    print('===')

Output: 输出:

>>> users = ['x1,x2']
>>> for user in users[0].split(','):
...    print(user)
...    print('===')


x1
===
x2
===

A per your comment: 根据您的评论:

from ast import literal_eval
users = ['[1,2]']

users = literal_eval(users[0])
for user in users:
    print(user)
    print('===')

Output: 输出:

1
===
2
===

For users = ['[x1,x1]'] : 对于users = ['[x1,x1]']

users = ['[x1,x1]']
users = users[0][1:-1]

for user in users.split(','):
    print(user)
    print('===')

It looks as is users is a list with a single item x1,x2 : 看起来users是一个包含单个项x1,x2列表

>>> users = ['x1,x2']
>>> for user in users:
...     print(user)
...     print("===")
... 
x1,x2
===

Others have pointed out that you seem to have a list containing a single string, ['x1, x2'] . 其他人指出,您似乎有一个包含单个字符串['x1, x2']

I took a look at the Django REST Framework internals, and it's definitely returning a list. 我看了看Django REST Framework的内部结构,它肯定会返回一个列表。

ListField inherits from Field , which defines a few methods, including run_validation , to_internal_value , and to_representation . ListField继承自Field ,该字段定义了一些方法,包括run_validationto_internal_valueto_representation

Two of those methods are abstract, and one of them, run_validation actually invokes the validation by calling self.to_internal_value(data) . 这些方法中有两个是抽象的,其中之一是run_validation实际上通过调用self.to_internal_value(data)来调用验证。

So to see what the validator is doing, we have to look at the ListField's implementation of to_internal_value . 因此,要查看验证程序的工作,我们必须查看ListField的to_internal_value实现。

The comment inside of to_internal_value says this: to_internal_value内部的注释是这样的:

"""
    List of dicts of native values <- List of dicts of primitive datatypes.
"""

Then it checks for invalid input types and finally, calls run_validation . 然后,它检查无效的输入类型,最后调用run_validation

According to my IDE, there are 5 implementations of run_validation in Django REST Framework. 根据我的IDE,Django REST Framework中有5种run_validation实现。 The most relevant one is probably ListSerializer . 最相关的一个可能是ListSerializer

5个run_validation的实现

The comments above ListSerializer tell us that we're probably in the right place: ListSerializer上方的ListSerializer告诉我们,我们可能在正确的位置:

# There's some replication of `ListField` here,
# but that's probably better than obfuscating the call hierarchy.

The ListSerializer class validates each item (source) and then appends it to a list called ret . ListSerializer验证每个项目(源) ,然后将其追加到名为ret的列表中。 So we should be returning a list. 因此,我们应该返回一个列表。

The unsolved piece of the puzzle here is what your input is that is causing the output to be incorrect, but tracing through the call stack, the code appears to be working as intended. 难题中未解决的部分是您的输入是什么导致输出不正确,但是通过调用堆栈进行跟踪,代码似乎可以正常工作。

EDIT: 编辑:

Could it be that to_representation is flattening your list because it thinks it's a dictionary? 是否因为to_representation认为这是一本字典而使您的列表扁平化?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM