简体   繁体   English

如何始终根据登录的用户获取数据?

[英]how can i get always data based on the logged user?

I'm Working with Django / Rest Framework and I have multiple applications and I would like to avoid repeaters in my situation, I would like to get the logged user data without repeating this line each time:我正在使用 Django / Rest 框架,我有多个应用程序,我想在我的情况下避免重复,我想获取记录的用户数据,而不是每次都重复这一行:
queryset = queryset.filter(user = self.request.user)

Fixed by reading more in the documentation:通过阅读文档中的更多内容来修复:

def get_queryset(self):
    """
    This view should return a list of all the purchases
    for the currently authenticated user.
    """
    user = self.request.user
    return Contact.objects.filter(user=user)

and i started called the get_queryset() like that self.get_queryset()我开始像get_queryset()那样self.get_queryset()

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

相关问题 如何在 Django 中获取登录用户的用户名? - How can I get the username of the logged-in user in Django? 如何仅在Django中打印当前登录的用户数据? - How can I only print the the current logged in user data in Django? 如何将登录(经过身份验证)的用户分配给他们提交的表单数据? - How can I assign the logged in (authenticated) user to the form data they submit? 如果登录Django,如何获取用户ID? - How I get User ID if logged in Django? 如何查看用户是否使用 Authomatic 登录? - How can I see if a user is logged in with Authomatic? 如何让用户使用 SQLite 保持登录状态? - How can I keep a user logged in with SQLite? 如果我正在尝试使用 django 创建一个 diray 应用程序,我怎样才能让它根据当前登录的用户显示不同的日记条目? - If i am trying to create a diray app using django, how can i get it show show me different diary entries based on the current logged in user? Django当我使用基于类的视图时,如何在表单中传递request.user(当前登录的用户)? - Django How I can pass request.user(current logged user) in forms when I use class based view? 如何获取 Post.user 的数据而不是登录用户 - How to get data of Post.user not the logged in User 如何在Django views.py中获取登录用户的用户名 - How can I get the username of the logged-in user in Django views.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM