I’m using OneToOneField connected with User to do form submitting tasks. But I’m getting OperationalError. This is one app I’m using for providing form after login. The login process are done using another app successfully.
Models.py:
from django.db import models
from django.forms import ModelForm
from sorl.thumbnail import ImageField
from django.contrib.auth.models import User
class BasicModel(models.Model):
user = models.OneToOneField(User, unique=True, related_name='profile')
name = models.CharField(max_length=200)
dob = models.DateField()
photo = ImageField(upload_to='sample')
class BasicModelForm(ModelForm):
class Meta:
model = BasicModel
exclude = ('user',)
Views.py:
@login_required
def BasicView(request):
if request.method == 'POST':
form = BasicModelForm(request.POST, request.FILES, instance=request.user.profile)
if form.is_valid():
data = form.save()
im = get_thumbnail(data.photo, '100x100', crop='center', quality=99)
return preview(request, data.id, im)
else:
form = BasicModelForm()
return render_to_response("unnamed.html", {'form': form}, context_instance=RequestContext(request))
def preview(request, id, im):
obj = get_object_or_404(BasicModel, pk=id)
return render_to_response("preview.html", {'obj': obj, 'im': im})
Errors:
Exception Type: OperationalError
Exception Value: (1054, "Unknown column 'unnamed_basicmodel.user_id' in 'field list'")
Exception Location: /usr/lib/pymodules/python2.7/MySQLdb/connections.py in defaulterrorhandler, line 36
Traceback: form = BasicModelForm(request.POST, request.FILES, instance=request.user.profile)
It looks like you have added the user field to your model, but not added the column to the database.
The syncdb command will create new tables for you, but it does not add or modify columns in tables that already exist. You have a few options:
If you’re still developing your app, and don’t have any important data yet, the easiest thing to do is to drop the table then run syncdb again to recreate it.
If you can’t drop the table, then you need to manually run the SQL command to add the column.
You may also want to investigate south, a schema migration tool for Django.