【问题标题】:how does one get the profile from a user in DJango如何从 DJango 中的用户那里获取个人资料
【发布时间】:2017-10-17 19:50:16
【问题描述】:

我有如下代码供人们登录:

if request.method == 'POST':

username = request.POST.get('username')
password = request.POST.get('password')

user = authenticate(username=username, password=password)

if user:
    # Check it the account is active
    if user.is_active:

        # Log the user in.
        login(request, user)

我已扩展用户以创建用户配置文件。它包含其他信息。例如:地址、城市、州等。

如何从这种情况中提取用户配置文件?对于 Eclipse 下的 Java,如果我输入“user”,我会看到所有适用于对象“user”的 valid 方法。

PyCharm 似乎没有这样的功能。

话虽如此,如何找到与用户关联的个人资料信息?

TIA

以下是配置文件型号代码:

class UserProfileInfo (models.Model):
    class Meta:

        db_table = 'mstrauthdjprofile'
        db_tablespace = 'PSAPUSR1001'

    user = models.OneToOneField(User)

    restrole = models.BigIntegerField(blank=True, null=True, default=0)

    profilepic = models.ImageField(upload_to='profile_pics', blank=True)

    lastpgprocno = models.BigIntegerField(blank=True, null=True, default=-1)
    currentpgprocno = models.BigIntegerField(blank=True, null=True, default=-1)
    nextpgprocno = models.BigIntegerField(blank=True, null=True, default=1)

    reclocktype = models.BigIntegerField(blank=True, null=True, default=0)
    reclockid = models.BigIntegerField(blank=True, null=True, default=0)

    def __str__(self):
        return self.user.username

【问题讨论】:

  • 添加您的个人资料型号代码
  • @NeErAj KuMaR - 已将其添加到消息中。有什么想法吗?
  • 尝试user.userprofileinfo获取配置文件对象,第一个user应该是django默认用户实例

标签: python django pycharm profile


【解决方案1】:

OneToOneFields 始终可以从其对应方访问。

user = authenticate(username=username, password=password)
user.userprofileinfo

user = User.objects.first()
user.userprofileinfo

user_profile_info = UserProfileInfo.objects.first()
user_profile_info.user

您可以通过在代码中插入断点来使用 pdb 之类的调试器(我相信 PyCharm 应该有一种自动化的方式来处理这个问题):

import pdb; pdb.set_trace()

这将允许您与当前范围进行交互。您可以使用__dict__查看对象具有的属性

user.__dict__
{'username': 'user', 'id': 1, ...}

还可以考虑使用更强大/交互式的调试器,如 IPython 或 bpython,它们内置了自动完成功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2016-07-29
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多