【问题标题】:Django Error : userprofile matching query does not existDjango 错误:用户配置文件匹配查询不存在
【发布时间】:2013-12-05 11:58:01
【问题描述】:

我是 Django 的新手。我正在尝试创建用户配置文件。首先,我在 models.py 中创建了一个模型及其处理程序,如下所示..

class UserProfile(models.Model):
    user = models.ForeignKey(User,null=False)

    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

然后用

编辑 settings.py
AUTH_PROFILE_MODULE = "lib.UserProfile"

其中 lib 是根文件夹,其中包含 init.py 、models.py 和所有。

然后我删除了集合中的所有当前用户,当我从管理面板重新输入它们时,会自动创建一个新集合 lib_userprofile,其中包含我在模型中提到的字段。现在我创建了如下视图

  class CurrentUser(APIView):
        authentication_classes = (authentication.TokenAuthentication,)
        def get(self,request):
            if request.user.is_authenticated():
                        profile=request.user.get_profile()
                        return Response(profile)

但是给了我以下错误..

UserProfile matching query does not exist.
Request Method: GET
Request URL:    http://pawan.demoilab.pune/api/currentuser
Django Version: 1.3.7
Exception Type: DoesNotExist
Exception Value:    
UserProfile matching query does not exist.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/query.py in get, line 351
Python Executable:  /usr/bin/python
Python Version: 2.7.3
Python Path:    
['/var/www/pawan.demoilab.pune/web/api',
 '/usr/local/lib/python2.7/dist-packages/Fabric-1.8.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/paramiko-1.12.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/ecdsa-0.10-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pymongo-2.6.3-py2.7-linux-i686.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/pymodules/python2.7']

任何帮助将不胜感激..我发现了很多关于这个错误的问题,但我无法解决错误..

【问题讨论】:

  • UserProfile 应该与 UserOneToOne 关系。此外,配置文件是否实际创建?由于未提供名称,您在信号处理程序中的创建将失败。可能,您没有显示实际代码。
  • 好的,我把它改成了OneToOneField。当我从管理面板添加新用户时,它的 useprofile 是使用这些字段自动创建的 "_id": ObjectId("528da840ee4340252254e34b"), "name": "", "user_id": "528da840ee4340252254e34a" 我正在显示确切的代码.. 什么你没有找到实际的代码..
  • 我怀疑是因为,配置文件创建应该失败并且不允许当名称为空或空白时。
  • 我将 name 保留为 null 或空白的确切位置?
  • 任何建议 Rohan.. 我被困在这

标签: python django


【解决方案1】:

我已经解决了这个错误。我正在回答我自己的问题,因为它可能会帮助面临同样问题的人。

我在 get_profile 函数中遇到错误,所以我通过替换 user__id__exact=self.id with user = self.id 在 get_profile() 函数的第 383 行检查了位于 /contrib/auth/models.py 的 models.py 文件的代码,它工作正常。

【讨论】:

  • 所以你改变了核心 django 代码?我希望它现在已修复:)
猜你喜欢
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
  • 2015-03-13
  • 2021-07-19
  • 1970-01-01
  • 2013-07-22
相关资源
最近更新 更多