neurix
neurix

Reputation: 4316

Django: Django generated sql query causes error

Updated question:

Django is giving me the following sql query:

SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."username" = %s  args=('andrew',);

If I execute the sql query in the postgresql command line, I get the following error:

ERROR:  syntax error at or near "%"
LINE 1: ..." FROM "auth_user" WHERE "auth_user"."username" = %s  args=(...
                                                             ^

However, when I slightly modify the statement, then I get the cirrect result from postgresql.

SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."username" = 'andrew';

Is the Django generated query incorrect?


Hi Stackoverflow people,

Very simple code which drives me crazy:

I want to extract the user information from user_auth with

user = get_object_or_404(User, pk = request.user.pk)

However, I get an error message in Django:

'NoneType' object does not support item assignment

When I check the sql query and execute it in the psql command line, psql gives me also an error message, which makes me thinking that the statement might be incorrect.

psql statement is:

SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = %s ; args=(7,)

Why does it say %s in the WHERE statement? The user id is not a string.

I believe the solution must be very simple - but I can figure out what the issue is here. Thank you for your help and suggestions!

Additional explanation

I am using the django_social_auth package for the user authentification. The user will be directed to the dashboard site once the 3rd part cleared the credentials, therefore I would assume that request.user is not None.

Complete traceback

Environment:


Request Method: GET
Request URL: http://login.test.com:8000/profile/dashboard/

Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.gis',
 'django.contrib.messages',
 'django.contrib.markup',
 'django.contrib.staticfiles',
 'django.contrib.flatpages',
 'django.contrib.humanize',
 'guardian',
 'easy_thumbnails',
 'userena',
 'userena.contrib.umessages',
 'south',
 'django_extensions',
 'debug_toolbar',
 'social_auth',
 'djangoratings',
 'about',
 'apps.profiles',
 'apps.map',
 'apps.contact',
 ]
Installed Middleware:
['django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.doc.XViewMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware']


Traceback:
File "/Users/neurix/Development/test/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/neurix/Development/test/test/apps/profiles/views.py" in dashboard
  35.     extra_context['username'] = user.username

Exception Type: TypeError at /profile/dashboard/
Exception Value: 'NoneType' object does not support item assignment

views.py

...
31: print "user: %s" %(request.user.pk)
32: user = get_object_or_404(User, pk = request.user.pk)
33:
34: 
35: extra_context['username'] = user.username
36: if user.first_name:
37:     extra_context['name'] = user.first_name
...

Upvotes: 0

Views: 504

Answers (1)

Paulo Scardine
Paulo Scardine

Reputation: 77389

Could it be that request.user is None?

if request.user is not None:
    user = get_object_or_404(User, pk = request.user.pk)

Upvotes: 1

Related Questions