Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Error : userprofile matching query does not exist

Tags:

python

django

I am newbie to Django. I am trying to create UserProfile. First I created one model and its handler in models.py as follows..

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)

Then edited settings.py with

AUTH_PROFILE_MODULE = "lib.UserProfile"

where lib is root folder which contains init.py ,models.py and all.

Then I deleted all the current user in the collection , and when i reentered them from admin panel , a new collection lib_userprofile is automatically created with fields i mentioned in model. Now I crearted a view as follows

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

But is giving me following error..

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

Any help would be appreciated please.. I found out many questions on this error , but i was not able to resolve error..

like image 563
Pawan Avatar asked Nov 21 '13 06:11

Pawan


1 Answers

I have resolved the error. I am answering my own question as it might help someone facing same problem.

I encountered error in get_profile function , So I checked the code of models.py file located at /contrib/auth/models.py at 383 line in get_profile() function by replacing user__id__exact=self.id with user = self.id And it worked Fine.

like image 67
Pawan Avatar answered Sep 30 '22 16:09

Pawan