User Profiles "user.get_profile()" returns 'matching query does not exist.'

2,680 views
Skip to first unread message

mclovin

unread,
Sep 20, 2008, 6:48:32 PM9/20/08
to Django users
I have spent about 2 hours on this so far, but havent been able to get
it right, my model is:

(located in schedule/models.py, stripped down to just the profile)

from django.contrib.auth.models import User
class profile(models.Model):
user = models.ForeignKey(User, unique=True)
aim = models.CharField(max_length=100, blank=True)
website = models.CharField(max_length=100, blank=True)

and i have this as my setting:
AUTH_PROFILE_MODULE = 'schedule.profile'



I did a syncdb and it showed the profile successfully being added...

then i did in "python manage.py shell":
from django.contrib.auth.models import User
u = User.objects.get(pk=1)
print u.get_profile().aim

but I get this error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.5/site-packages/django/contrib/auth/
models.py", line 293, in get_profile
self._profile_cache =
model._default_manager.get(user__id__exact=self.id)
File "/usr/lib/python2.5/site-packages/django/db/models/manager.py",
line 93, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/db/models/query.py",
line 303, in get
% self.model._meta.object_name)
DoesNotExist: profile matching query does not exist.



anyone have any idea what is wrong?


Paddy Joy

unread,
Sep 20, 2008, 9:14:42 PM9/20/08
to Django users
The error message is telling you that the user has no profile
(Specifically there is no record in the table schedule.profile related
to the user).

Your app needs to take care of creating the user profile, even if it
going to be empty. What I have done in my case is I force the user to
create a profile when they first log on by checking if the profile
exists.

# Check if logged in user has profile, if not redirect to
create profile
try:
profile = request.user.get_profile()
except:
return HttpResponseRedirect("/createprofile/")


Paddy

mclovin

unread,
Sep 20, 2008, 10:21:44 PM9/20/08
to Django users
That fixed it! I thought it automatically created and attached one
which is why i was so confused.

Brian Sanchez

unread,
Mar 28, 2014, 2:50:05 PM3/28/14
to django...@googlegroups.com, padd...@gmail.com
try:

except:

it worked
thank you!!
Reply all
Reply to author
Forward
0 new messages