Django HTTP请求get vs getlist行为(Django HTTP Request get vs getlist behavior)

编程入门 行业动态 更新时间:2024-10-25 02:30:26
Django HTTP请求get vs getlist行为(Django HTTP Request get vs getlist behavior)

我有一个Django表单,提交了一个值列表给我的视图。 我首先尝试使用get方法检索列表,但发现它只返回了最后一个,我应该使用getlist 。 经过一些磕磕绊绊之后,我发现了一个封闭的Django bug ,它解释了这种行为的动机:

这背后的原因是API方法应该始终返回字符串或列表,但不能同时返回两者。 Web应用程序中的常见情况是表单键与单个值相关联,因此这是[]语法的作用。 当你打算多次使用一个键作为单个值时,getlist()适用于那些场合(比如你的)。

我只是想知道这是否真的是一种最佳实践 - 它与get方法在其他数据结构上的工作方式相矛盾,即。 字典。

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请求确实支持分配给一个参数(键)的多个值。 这就是人们可以使用它们并且(有时)使用它们的原因。 这也是Django引入MultiValueDict结构的原因。

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

get()总是返回列表。

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

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

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

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

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()总是返回单个值。 那么在这种情况下如何在没有getlist的情况下处理多个值?

所以为了回答你的问题, get/getlist行为有附加价值。

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.

更多推荐

本文发布于:2023-08-07 23:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466405.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HTTP   Django   getlist   behavior   Request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!