5

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..

5
  • 1
    UserProfile should have OneToOne relation with User. Also, is the profile actually created? Your create in signal handler will fail as no name is provided. Probably, you are not showing the actual code. Commented Nov 21, 2013 at 6:48
  • 1
    Ok, I changed it to OneToOneField. When I add new user from admin panel It's useprofile is automatically created with these fields "_id": ObjectId("528da840ee4340252254e34b"), "name": "", "user_id": "528da840ee4340252254e34a" And I am showing exact code.. What code you are not finding actual one.. Commented Nov 21, 2013 at 6:51
  • I'm suspecting because, profile create should fail and do not allow when name is null or blank. Commented Nov 21, 2013 at 6:57
  • Exactly where i have kept name null or blank? Commented Nov 21, 2013 at 7:00
  • Any suggestions Rohan.. I am stuck in this Commented Nov 21, 2013 at 7:17

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So you changed core django code? I hope it is fixed now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.