Django错误:userprofile匹配查询不存在

时间:2013-11-21 06:13:13

标签: python django

我是Django的新手。我正在尝试创建UserProfile。首先,我在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和all。

然后我删除了集合中的所有当前用户,当我从管理面板重新输入它们时,会自动创建一个新集合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']

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

1 个答案:

答案 0 :(得分:8)

我已经解决了这个错误。我正在回答我自己的问题,因为这可能有助于遇到同样问题的人。

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