CuriousCoder
CuriousCoder

Reputation: 491

Not able to use User in ForeignKey in Django

I am creating a web application in Django where a user can store files in his/her account. I get ProgrammingError when I want to use User as a ForeignKey.

Here is the code in models.py

from django.db import models
from django.contrib.auth.models import User

class InputFiles(models.Model):
    input_user = models.ForeignKey(User)
    audio_file = models.FileField(upload_to='\media')
    text_file = models.CharField(max_length=200) 

This table can be seen through the admin page. When I comment the input_user (I am using South to update the tables), I am able to see this table through admin page. But, when I comment the input_user, I get the following error:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/nalign_app_recorder/inputfiles/

Django Version: 1.6.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gunicorn',
'nalign_app_recorder',
'south')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
112.                     response = wrapped_callback(request, *callback_args,              **callback_kwargs)
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
432.                 return self.admin_site.admin_view(view)(*args, **kwargs) 
File "/usr/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
99.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/views/decorators/cache.py" in    _wrapped_view_func
52.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
198.             return view(request, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
29.             return bound_func(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
99.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
25.                 return func(self, *args2, **kwargs2)
File "/usr/lib/python2.7/dist-packages/django/contrib/admin/options.py" in    changelist_view
1411.             'selection_note': _('0 of %(cnt)s selected') % {'cnt':   len(cl.result_list)},
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py" in __len__
77.         self._fetch_all()
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py" in _fetch_all
854.             self._result_cache = list(self.iterator())
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py" in iterator
220.         for row in compiler.results_iter():
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in results_iter
710.         for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
781.         cursor.execute(sql, params)
File "/usr/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
69.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
53.                 return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/dist-packages/django/db/utils.py" in __exit__
99.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
53.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /admin/nalign_app_recorder/inputfiles/
Exception Value: column nalign_app_recorder_inputfiles.input_user_id does not exist
LINE 1: SELECT "nalign_app_recorder_inputfiles"."id", "nalign_app_re...

Does anyone know the solution for this.

Upvotes: 0

Views: 119

Answers (2)

CuriousCoder
CuriousCoder

Reputation: 491

I finally found the answer.

I guess the problem came up as south could not add new entries. Thus, I cleared the database and started again. The problem disappeared when I did the following:

  1. First remove the migrations folder
  2. Then remove the content in the data completely
  3. run 'syncdb'
  4. Finally, convert to south
  5. Of course, don't forget to create a super user if you want to log into the admin page

The commands are as follows:

sudo rm -r myapp/migrations/
sudo python manage.py sqlflush | python manage.py dbshell
sudo python manage.py syncdb 
sudo python manage.py convert_to_south myapp
sudo python manage.py createsuperuser

Upvotes: 2

dragonx
dragonx

Reputation: 15143

The InputFiles table doesn't have a column for input_user. Try doing a 'manage.py syncdb' to update the database..

Upvotes: 0

Related Questions