0

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) 
rnk
  • 2,174
  • 4
  • 35
  • 57

1 Answers1

3

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thanks @Alasdair. But now I'm getting Basic model matching query does not exist. – rnk Mar 05 '12 at 09:40
  • DoesNotExist at /unnamed/ Exception Value: BasicModel matching query does not exist. Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/query.py in get, line 349 Traceback: form = BasicModelForm(request.POST, request.FILES, instance=request.user.profile) – rnk Mar 05 '12 at 09:41
  • The problem is you're doing `instance=request.user.profile` before you have created the profile. If the profile does not exist yet, you can't pass it as the instance. Save with `commit=False`, and set the user then save. See this [answer](http://stackoverflow.com/a/9332588/113962) for an example. – Alasdair Mar 05 '12 at 10:34