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.
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.sqlite3python manage.py makemigrationspython manage.py migratepython manage.py loaddata <fixture-name> to reload old dataI 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With