gpt4 book ai didi

python - Django 错误 : userprofile matching query does not exist

转载 作者:太空狗 更新时间:2023-10-30 00:50:35 24 4
gpt4 key购买 nike

我是 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 和所有内容的根文件夹。

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

  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']

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

最佳答案

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

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

关于python - Django 错误 : userprofile matching query does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20113779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com