Django HTTP Request get vs getlist行为 [英] Django HTTP Request get vs getlist behavior

查看:1279
本文介绍了Django HTTP Request get vs getlist行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django表单提交了一个值列表给我的看法。我首先尝试使用 get 方法检索列表,但发现它只返回最后一个,我应该使用 getlist 。在一些绊脚石之后,我发现一个关闭的Django错误解释了这种行为的动机:


背后的原因是API方法应该始终返回字符串或列表,但不能同时返回。 Web应用程序中的常见情况是将表单键与单个值相关联,这就是[]语法所做的。当你打算多次使用一个单一的值时,getlist()就是这样的场合(像你的)。


只是想知道这是否实际上是一个最佳实践 - 它与get方法在其他数据结构上的工作方式相矛盾。

解决方案

HTTP请求确实支持分配给一个参数(键)的多个值。这就是为什么人们可以使用它们(有时)使用它们。这也是为什么Django引入了 MultiValueDict 结构



分成 get() getlist() / code>是有益的,因为它可以帮助您避免错误,并使您的视图代码简单。考虑其他行为,他们都需要更多的代码来做同样的事情:




  • get() 总是返回列表。



    在大多数用例中,只传递一个值到一个键,因此您需要添加 [0] 并提供默认值作为列表。



    param = request.GET.get('param',['default value',])[0]


  • get()返回单个值或列表,具体取决于值的数量。 p>




HTML选择中允许多个选项是一个缺点。人们可以选择零个,一个或多个值。这意味着您需要将单个值转换为列表或反向自己:

  params = request.GET.get ('params',[])
#这里你绝对不知道这是一个列表还是一个值
#但是你只需要一种类型

#如果需要列表:---------------------------------
如果不是isinstance(params,list )
params = [params,]

objs = TestModel.objects.filter(id__in = params).all()

#如果您需要单一值:-------------------------
if isinstance(params,list):
params = params [0]#错误如果params是空列表...


obj = TestModel.objects.get(id = params)




  • get()始终返回单个值。那么在这种情况下,如何处理多个值呢?没有 getlist



为了回答你的问题,有一个附加值 get / getlist 行为。


I had a Django form that submitted a list of values to my view. I first tried retrieving the list using the get method but discovered that it only returned the last one and I should be using getlist. After some stumbling around I found a closed Django bug that explained the motivation for this behavior:

The reasoning behind this is that an API method should consistently return either a string or a list, but never both. The common case in web applications is for a form key to be associated with a single value, so that's what the [] syntax does. getlist() is there for the occasions (like yours) when you intend to use a key multiple times for a single value.

I'm just wondering whether this is actually a best practice - it contradicts the way the get method works on other data structures, ie. dictionaries.

解决方案

HTTP requests do support multiple values assigned to a one parameter (key). That's why people can use them and do (sometimes) use them. That's also why Django introduced the MultiValueDict structure.

Division into get() and getlist() is beneficial, because it helps you avoid errors and keeps your view code simple. Consider other behaviors, they all require more code to do exactly the same thing:

  • get() always returning list.

    In most use cases you pass just one value to a one key, so you would need to add [0] and provide default value as a list.

    param = request.GET.get('param', ['default value',])[0]

  • get() returning single value or a list, depending on number of values.

It is a drawback in HTML selects with multiple options allowed. People can select zero, one or more values. That means you'll need to convert single value to list or in opposite direction by yourself:

 params = request.GET.get('params', [])
 # Here you have absolutely no idea if this is a list or a single value
 # But you will need only one of that types

 # If you need list: ---------------------------------
 if not isinstance(params, list):
     params = [params,]

 objs = TestModel.objects.filter(id__in=params).all()

 # If you need single value: -------------------------
 if isinstance(params, list):
     params = params[0]   # Error if params is empty list...


obj = TestModel.objects.get(id=params)

  • get() always returning single value. So how do you handle multiple values without getlist in that case?

So to answer your question there is an added value of get/getlist behaviour.

这篇关于Django HTTP Request get vs getlist行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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