【问题标题】:Passing User Data to Admin Actions in Django将用户数据传递给 Django 中的管理操作
【发布时间】:2020-08-06 00:48:29
【问题描述】:

我正在尝试编写一个访问选定用户的数据的管理操作。即用户的电子邮件。但是,我只能访问当前登录用户的实例/数据。

例如,要访问选定用户的电子邮件,我尝试过:

#models.py

class Account(AbstractBaseUser):
    email = models.EmailField(max_length=60, unique=True)

#admin.py

from account.models import Account

for Account in queryset:
    author = request.Account.email
    #OR     
    author = Account.objects.get(email=request.email)
    print(author)

这两个都会用试图提取数据的管理员的电子邮件地址填写“作者”。

有谁知道我如何通过管理员操作从选定帐户中提取数据?

【问题讨论】:

  • 尝试author = Account.objects.get(email=request.user.email) 在没有for循环的一行中。此外,您将需要进行自定义身份验证。更多here
  • 感谢您的想法,但这给了我与上述相同的结果

标签: python django django-models django-admin django-admin-actions


【解决方案1】:

我真的把它复杂化了。具有讽刺意味的是,我在这个名为simpleisbetterthatcomplex 的网站上找到了答案。正确的格式是

for Account in queryset:
    print(Account.email)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 2022-07-22
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2021-03-16
    相关资源
    最近更新 更多