Reputation: 15
I am a beginner working on my first "independent" project without tutorial guidance. I have been working on a two password confirmation form for my registration page, without utilizing the django.contrib.auth.forms UserCreationForm. I have my Profile model with one attribute of 'user' which is a OneToOneField with the django.contrib.auth.models User model. For some reason, after I fill out my registration form from the front end and check the profiles group, the OperationalError pops up. This is not the first time I have had a 'no such column.' Can someone help? Would be greatly appreciated.
My User/form.py,
from django import forms
from django.contrib.auth.models import User
class UserRegisterForm(forms.ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'password']
def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords do not match")
return password2
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
My User/views.py, register view, which is my registration form.
def register(request):
form = UserRegisterForm(request.POST or None)
if form.is_valid():
user = form.save(commmit=False)
user = form.save()
raw_password = form.cleaned_data.get('password1')
user = authenticate(request, password=raw_password)
if user is not None:
login(request, user)
return redirect('login')
return render(request, 'User/register.html', {'form': form})
My very simple User/models.py ,
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
My User/migrations/0001_initial.py,
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
If you have anything to contribute, I would gladly take any information provided. Thank you.
Edit:
Full error Traceback:
Environment:
Request Method: GET
Request URL: http://localhost:8000/admin/User/profile/
Django Version: 3.0.6
Python Version: 3.8.2
Installed Applications:
['Blog.apps.BlogConfig',
'User.apps.UserConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'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 (most recent call last):
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
The above exception (no such column: User_profile.user_id) was the direct cause of the following exception:
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\contrib\admin\options.py", line 607, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\contrib\admin\sites.py", line 231, in inner
return view(request, *args, **kwargs)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\contrib\admin\options.py", line 1796, in changelist_view
'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\models\query.py", line 258, in __len__
self._fetch_all()
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\models\query.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1151, in execute_sql
cursor.execute(sql, params)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /admin/User/profile/
Exception Value: no such column: User_profile.user_id
Upvotes: 0
Views: 3630
Reputation: 1
I too faced the same error. You should make the migrations to change the model structure.
Run the commands:
python manage.py makemigrations
python manage.py migrate
Upvotes: 0
Reputation: 2018
I think in class Meta
of your UserRegistrationForm
you forgot to add your password fields correctly
this should be:
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'password1','password2']
Upvotes: 1