Reputation: 2929
I'm tearing my hair out trying to figure out why my custom user profile view no longer works. I'm using django-allauth, and have customised my user profile by creating a model with one-to-one field to the user. However, my view is throwing the error:
OperationalError at /profile/
no such table: user_profile
Request Method: GET
Request URL: http://localhost:8000/profile/
Django Version: 1.7.4
Exception Type: OperationalError
Exception Value:
no such table: user_profile
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py in execute, line 485
Python Executable: /usr/bin/python
Python Version: 2.7.6
My models.py with the custom user profile looks like this:
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.db import models
from allauth.account.models import EmailAddress
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
# user visible settings
stop_reminders = models.BooleanField ( default=False,
help_text = 'Flag if user wants reminders not to be sent.' )
stop_all_email = models.BooleanField ( default=False,
help_text = 'Flag if user wants no email to be sent.' )
# hidden settings
is_premium = models.BooleanField ( default=False,
help_text = 'Flag if user has the premium service.' )
def __unicode__(self):
return "{}'s profile".format(self.user.username)
class Meta:
db_table = 'user_profile'
def account_verified(self):
if self.user.is_authenticated:
result = EmailAddress.objects.filter(email=self.user.email)
if len(result):
return result[0].verified
return False
#User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
My view, which is a place to set user profile settings looks like this:
@login_required
def user_settings (request):
"""
User settings view, handles changing of user settings and
entry distribution (eventually)
"""
if request.method == 'POST': # If the form has been submitted...
form = UserProfileForm(request.POST, instance=request.user.profile) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
form.save ()
return render_to_response('sm/profile_update_success.html',
{},
context_instance=RequestContext(request))
else:
# create unbound form with initial values generated
# from the user's profile
form = UserProfileForm (instance=request.user.profile)
return render ( request, 'profile.html', { 'form': form, } )
Until recently this was working fine, and I'm not sure what has changed to make it stop. Can anyone explain please?
I'm using Django-1.7.4 and django-allauth-0.19.1. For debug I'm using sqllite, which I have tried creating using both python manage.py migrate
and python manage.py syncdb
.
Upvotes: 4
Views: 11127
Reputation: 477
I had the same problem but for me the solution was different. I had accidentally deleted the __init__.py file which is required by django in the migrations directory while cleaning which meant that some migrations were not running producing the same error. Replacing it solved the issue.
Upvotes: 4
Reputation: 51
Okay its looks like you forget to run the migrations command; just go ahead and run the following command; in the Django shell:
$ python manage.py migrate
Then,
$ python manage.py makemigrations
Upvotes: 1
Reputation: 11
I had the same issue. To fix it I exit out of the server by Control+C. Than migrate by running these two commands: $ python manage.py makemigrations it tells me Migrations for 'profiles':
profiles/migrations/0001_initial.py
- Create model profile
than I run $ python manage.py migrate
it tells me
Operations to perform: Apply all migrations: admin, auth, contenttypes, profiles, sessions Running migrations:
Applying profiles.0001_initial... OK
now I run the server again $ python manage.py runserver
and when I create the profile it shows
[24/Aug/2018 19:36:16] "GET /admin/profiles/profile/add/ HTTP/1.1" 200 4381
[24/Aug/2018 19:36:19] "POST /admin/profiles/profile/add/ HTTP/1.1" 302 0
[24/Aug/2018 19:36:19] "GET /admin/profiles/profile/ HTTP/1.1" 200 4376
[24/Aug/2018 19:36:19] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[24/Aug/2018 19:36:19] "GET /static/admin/css/changelists.css HTTP/1.1" 200 6170
[24/Aug/2018 19:36:19] "GET /static/admin/img/tooltag-add.svg HTTP/1.1" 200 331
[24/Aug/2018 19:36:19] "GET /static/admin/img/icon-yes.svg HTTP/1.1" 200 436
and profile is created. Hope that help.
Upvotes: 1
Reputation: 2929
Gahh, the answer was that I had commented out my main app's entry from INSTALLED_APPS
. Which is silly.
Upvotes: 2
Reputation: 1901
First of all, if it worked before, I can't explain why it no longer is.
You should inspect the sqlite file itself, and see if the table is there. (The file is usually found at the root as db.sqlite3
, and can be opened with any standard sqlite browser)
You mentioned you've been running migrate, but is the actual migration there already?
Run python manage.py makemigrations
first if you haven't. If need be, verify the migration file itself.
There's also the python manage.py inspectdb
command that could give you some insight to the models you're using.
If still nothing, you could of course start from scratch. I've probably missed a shortcut somewhere, but I'd:
python manage.py dumpdata --indent=4 <appname>
) (Save this into fixtures)db.sqlite3
python manage.py makemigrations
python manage.py migrate
python manage.py loaddata <fixture-name>
to reload old dataUpvotes: 4