Following some Django tutorials and getting a weird error I can not get my head around. I've migrated the database a few times previously, so before I start I delete all the files in the apps 'migrations' folder. Here is my models.py file:
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, primary_key=True)
SUBSCRIPTION_PLANS = (
('DEMO', 'Free Trial'),
('MONTH','Monthly'),
('YEAR','Yearly'),
('SIXMONTH','Six month plan'),
)
profile_pic = models.ImageField(upload_to = 'profile_pics', blank = True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
subscription_plan = models.CharField(max_length=10, choices=SUBSCRIPTION_PLANS, default = 'DEMO')
def __str__(self):
return self.user.username
Here is the view.py file:
from django.shortcuts import render
from UserProfile.forms import UserForm,UserProfileForm
# Create your views here.
def register(request):
registered = False
if request.method == 'POST':
user_form = UserForm(request.POST)
profile_form = UserProfileForm(data=request.POST)
if user_form.is_valid() and profile_form .is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
if 'profile_pic' in request.FILES:
print 'has profile pic'
profile.profile_pic = request.FILES['profile_pic']
profile.save()
registered = True
else:
print(user_form.errors, profile_form.errors)
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render(request, 'UserProfile/registration.html',{'user_form':user_form,
'profile_form':profile_form,
'registered':registered})
I run >>python manage.py makemigrations UserProfile
and get:
Migrations for 'UserProfile':
UserProfile/migrations/0001_initial.py:
- Create model Profile
Here is forms.py:
class UserProfileForm(forms.ModelForm):
class Meta():
model = Profile
fields = ('subscription_plan', 'first_name', 'profile_pic', 'last_name')
I run:
>>python manage.py migrate UserProfile
Operations to perform:
Apply all migrations: UserProfile
Running migrations:
No migrations to apply.
I try to fill in the form and I still get the error: no such column: profile_pic
OperationalError at /user_profile/register
no such column: profile_pic
I clearly created the profile_pic column in the model and migrated it. So why does Django think it is not there?
I made an another attempt to recreate the table from scratch:
python manage.py migrate --fake UserProfile zero
Operations to perform:
Unapply all migrations: UserProfile
Running migrations:
Rendering model states... DONE
Unapplying UserProfile.0001_initial... FAKED
python manage.py makemigrations UserProfile
Migrations for 'UserProfile':
UserProfile/migrations/0001_initial.py:
- Create model Profile
python manage.py migrate --fake-initial UserProfile
Operations to perform:
Apply all migrations: UserProfile
Running migrations:
Applying UserProfile.0001_initial... FAKED
Still the same error.
Running python manage.py sqlmigrate UserProfile 0001_initial
BEGIN;
--
-- Create model Profile
--
CREATE TABLE "UserProfile_profile" ("user_id" integer NOT NULL PRIMARY KEY REFERENCES "auth_user" ("id"), "profile_pic" varchar(100) NOT NULL, "first_name" varchar(30) NOT NULL, "last_name" varchar(30) NOT NULL, "subscription_plan" varchar(10) NOT NULL);
COMMIT;
What the hell? The 'profile_pic' field is in the SQL! So what's wrong then?
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/ilyalapan/Documents/Development/OE Ventures/businessMaps/businessMaps/UserProfile/views.py" in register
26. profile.save()
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save
796. force_update=force_update, update_fields=update_fields)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save_base
824. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in _save_table
889. forced_update)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in _do_update
939. return filtered._update(values) > 0
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _update
654. return query.get_compiler(self.db).execute_sql(CURSOR)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
1148. cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
835. cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
337. return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /user_profile/register
Exception Value: no such column: profile_pic
UPDATE: I've commented out some the fields from the model, but then I would get the same error with a different field. So if it's not 'profile_pic', it's subscription plan. Some more info - here's forms.py:
from django import forms
from django.contrib.auth.models import User
from UserProfile.models import Profile
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
#TODO: Haven't figured out why this is needed yet. Will check docs
class Meta():
model = User
fields = ('username', 'email', 'password')
class UserProfileForm(forms.ModelForm):
class Meta():
model = Profile
fields = ('first_name', 'last_name' ,'profile_pic', 'subscription_plan')
I will try deleting the Django app and starting again from scratch.