【问题标题】:Django: Access Admin User in Python ShellDjango:在 Python Shell 中访问管理员用户
【发布时间】:2013-07-11 08:13:21
【问题描述】:

我正在使用 Django Userena,但是当尝试登录 django 管理屏幕时,userena 正在制造麻烦:Profile matching query does not exist

如何使用 shell 为管理员用户创建配置文件?这是我的个人资料模型,但我不知道如何访问管理员用户的密钥来为其创建个人资料对象:

class Profile(UserenaBaseProfile, FacebookProfileModel):
    user = models.OneToOneField(User,
                                unique=True,
                                verbose_name=('user'),
                                related_name='my_profile')
    website = models.URLField(null=True, blank=True)

我知道这可能很简单,但到目前为止,谷歌让我失望了......谢谢你的想法!

PS。我以前可以毫无问题地登录到我的管理屏幕,但现在发生了一些变化,我收到了Profile matching query does not exist 错误(我也没有更改代码).. 任何我现在无法登录的想法也受到赞赏。谢谢!

【问题讨论】:

    标签: python django profile


    【解决方案1】:

    你可以这样做:

    $ ./manage.py shell
    > from django.contrib.auth.models import User
    > from <profile_package>.models import Profile
    > user = User.objects.get(username='<admin_user_name>')
    > profile = Profile(user=user)
    > profile.save()
    

    【讨论】:

    • 请注意,从 django 1.9 开始,这已被弃用:使用此方法将得到:AttributeError: Manager is not available; 'auth.User' 已替换为 'user.User'。改为使用:from django.apps import appsUser = apps.get_model('user', 'User')。然后继续。
    • 这对于在已经有没有用户的网站上创建夹层配置文件时也很有帮助。
    猜你喜欢
    • 2016-07-05
    • 2019-08-11
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多