LinkedHashMap中的entrySet()是否也保证订单?(Does entrySet() in a LinkedHashMap also guarantee order?)
当有人尝试访问它时,我使用linkedHashMap来保证顺序。 然而,当它来到迭代的时候,使用entrySet()返回键/值对来保证顺序呢? 迭代时不会发生任何变化。
编辑:还有,迭代遍历地图是否有任何不利影响,通过迭代键和调用get?
I am using a linkedHashMap to guarantee order when someone tries to access it. However, when it comes time to iterate over it, does using entrySet() to return key/value pairs guarantee order as well? No changes will be made while iterating.
EDIT: Also, are there any adverse effects from iterating through the map by iterating through its keys and calling get?
原文:https://stackoverflow.com/questions/1190083
最满意答案
那么,最后,我继承了REST Framework TokenAuthentication,并在urls文件中指向它
url(r'^api-token-auth/', back_views.TokenAuthenticationView.as_view()),
并且视图处理请求并手动调用update_last_login,如下所示:
from django.contrib.auth.models import update_last_login class TokenAuthenticationView(ObtainAuthToken): """Implementation of ObtainAuthToken with last_login update""" def post(self, request): result = super(TokenAuthenticationView, self).post(request) try: request_user, data = requests.get_parameters(request) user = requests.get_user_by_username(data['username']) update_last_login(None, user) except Exception as exc: return None return result
Well, at the end I inherited from the REST Framework TokenAuthentication, pointing to it in the urls file
url(r'^api-token-auth/', back_views.TokenAuthenticationView.as_view()),
and the View handles the request and manually calls the update_last_login like this:
from django.contrib.auth.models import update_last_login class TokenAuthenticationView(ObtainAuthToken): """Implementation of ObtainAuthToken with last_login update""" def post(self, request): result = super(TokenAuthenticationView, self).post(request) try: request_user, data = requests.get_parameters(request) user = requests.get_user_by_username(data['username']) update_last_login(None, user) except Exception as exc: return None return result
相关问答
更多-
那么,最后,我继承了REST Framework TokenAuthentication,并在urls文件中指向它 url(r'^api-token-auth/', back_views.TokenAuthenticationView.as_view()), 并且视图处理请求并手动调用update_last_login,如下所示: from django.contrib.auth.models import update_last_login class TokenAuthenticationView( ...
-
使用Django REST Framework进行身份验证返回405(Authenticating with Django REST Framework returns 405)[2023-12-24]
curl响应与AJAX响应相同。 curl响应返回301状态代码而不是405,因为您使用了不同的URL(没有尾部斜杠)。 问题是你只能POST到/api-token-auth/ URL,没有实现GET方法。 正如Django REST框架API令牌认证文档所说,用例是这样的: 您必须将username和password POST到/api-token-auth/ - 以获取身份验证令牌。 要访问需要身份验证的URL,您必须在Authorization HTTP标头中包含该标记。 这意味着您必须测试令牌身份验 ... -
如何在Django Rest Framework中使用自定义标记模型(How to use custom token model in Django Rest Framework)[2022-02-13]
该消息所说的是模型Token可以与任何其他模型交换,只要它具有属性key和user 。 这样,例如,如果您想要一种更复杂的方式来生成令牌密钥,您可以定义自己的模型。 因此,如果您需要自定义令牌模型,则应执行以下操作: 从rest_framework.authtoken.models子类化Token模型。 在此处添加您想要的任何自定义行为,但请确保它具有key属性和user属性。 从rest_framework.authentication对TokenAuthentication类进行子类rest_frame ... -
使用FileUploadParser ,它全部在请求中。 使用put方法,你会在docs中找到一个例子:) class FileUploadView(views.APIView): parser_classes = (FileUploadParser,) def put(self, request, filename, format=None): file_obj = request.FILES['file'] # do some stuff with up ...
-
不,不是在你的models.py - 在模型方面的事情,所有你需要做的是在你的INSTALLED_APPS包含适当的应用程序( rest_framework.authtoken )。 这将提供与用户外键的令牌模型。 您需要做的是决定何时以及如何创建这些令牌对象。 在您的应用程序中,每个用户是否自动获取令牌? 还是只有某些授权用户? 或者只有当他们特别要求一个? 如果每个用户都应该有一个令牌,那么您链接到的页面上会显示一段代码,它会显示如何设置信号以自动创建它们: @receiver(post_save, s ...
-
基本上......我的问题有点愚蠢,你可以根据需要实现权限来解决这个问题。 问题解决了! :-) Basically... my question is kind of stupid and you can solve this with implementing permissions as needed. Problem solved! :-)
-
Django如何在TokenAuthentication中使用login_required(Django how to use login_required with TokenAuthentication)[2023-09-23]
用例是错误的。 Django rest框架不允许这些东西。 http://www.django-rest-framework.org/topics/ajax-csrf-cors/#javascript-clients 会话身份验证应该用于Web浏览器。 在将它用于移动设备时不需要它们。 使用令牌认证时,休息框架视图会处理csrf验证。 The use case is wrong. Django rest framework doesn't allow these things. http://www.dja ... -
你可以使用一些装饰器来做到这一点:) from django.contrib.auth import authenticate def mock_funcname(func): def wrapper(request, *args, **kwargs): user = authenticate(token=request.REQUEST['token']) if user.id == 'TEST USER ID': return Respon ...
-
obtain_jwt_token只是对视图的引用,与任何视图一样,您可以将其子类化并引用它 class MySpecialJWT(ObtainJSONWebToken): def post(self, request, *args, **kwargs): response = super().post(request, *args, **kwargs) # foo bar return response obtain_jwt_token is jus ...
-
这个问题的答案可以在这里找到: 简而言之:此令牌的有效期非常长。 如果您想拥有在一定时间后过期的令牌,请参考上述问题中显示的示例。 Answer to this question can be found HERE: SHORTLY: this token is valid inifintly long. If you would like to have token, which expires after certain time, please refere to examples shown in ...