1

I'm currently working on my first Django project and have hit a roadblock in regard to adding an ImageField to an existing model.

I currently have a model for a user posting content and one for django-profiles. For the former, I added an ImageField so the used can submit a photo to accompany the text content. For the latter, I want to allow users to upload a profile photo (not worried at the moment about avatar resizing).

Things were working well until I added ImageFields to both models. I then synced the database with South. What resulted when I try to pull up the once working templates for profiles is:

"no such column: report_userprofile.profile_pic"

A similar error occurs when trying to view the submitted text content.

I set both of the fields to "blank=True" so it seemed as if not having an image file would not cause an error. I'm obviously wrong.

I did not initially put these fields in the models, which in hindsight was a big mistake. I've also just set up the MEDIA_ROOT and MEDIA_URL as follows:

MEDIA_ROOT = 'os.path.join(BASE_DIR, "media")'

MEDIA_URL = 'http://localhost:8000/media'

The models I'm now using are:

class Story(models.Model):
title = models.CharField(max_length=100)
topic = models.CharField(max_length=50)
copy = models.TextField()
author = models.ForeignKey(User)
zip_code = models.CharField(max_length=10)
latitude = models.FloatField(blank=False, null=False)
longitude = models.FloatField(blank=False, null=False)
date = models.DateTimeField(auto_now=True, auto_now_add=True)
pic = models.ImageField(upload_to='pictures', blank=True)   
def __unicode__(self):
    return " %s" % (self.title)

class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
first_name = models.CharField(max_length=25)
last_name = models.CharField(max_length=35)
email = models.EmailField()
birth_date = models.DateField(blank=True, null=True)
city = models.CharField(max_length=25)
state = models.CharField(max_length=20)
zip_code = models.CharField(max_length=10)
profile_pic = models.ImageField(upload_to='pictures', blank=True)

The URLs file is set up as follows:

    url(r'^admin/', include(admin.site.urls)),

(r'^media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': settings.MEDIA_ROOT},

The core problem here is I have no idea what the error might be (the previous templates even?). Or better yet, errors, as I'm sure I've made several. It's a bit of a soup right now along with the resulting distress.

Just as an FYI, I have installed the PIL.

Any insight at all, even small insight, into what I need to do to remedy this situation would be beyond appreciated. I've searched for similar issues but none this specific emerges. (The first lesson for the future would be to obviously set up the ImageFiles when initially assembling the models.)

Many thanks.

Why Not
  • 603
  • 3
  • 14
  • 25

2 Answers2

4

This is exactly as you would expect if you add a column that doesn't exist in the database (syncdb does not update database columns).

If this is a question about django south, you should add it to the tags and add more background about what you did in south that failed.

This should be a simple operation for south:

  1. Comment out the ImageField
  2. Run python manage convert_to_south myapp (or manage.py schemamigration myapp --initial, then then python manage migrate myapp --fake)
  3. Uncomment ImageField;
  4. Run python manage schemamigration myapp --auto
  5. Run python manage migrate myapp

If you're just trying to make it work without South you have 2 other options..

Manually add the column to your database or reset the database table manage.py reset myapp - removes all data.

Manually modifying the DB is very easy if you run the python manage.py sqlall myapp command -- it will list the SQL used to build the table, so you can copy and paste the missing field line into a python manage.py dbshell session (in your case, sqlite3) if you convert the key parts to an alter table statement.

Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
0

If you've altered the class structure, you will need to rebuild the database or alter the database tables to reflect the changes you've made to your class. The error is telling you exactly what the problem is. The column isn't in the table. syncdb won't do it. The easiest way to do it is:

manage.py reset table_name

table_name is {app}_{class}. This will delete all the current data. If you want to alter the data in place, you'll need to alter it within whatever database shell you're using.

Carl F.
  • 6,718
  • 3
  • 28
  • 41
  • Thanks. So this will reset the entire "Story" class, leaving it empty (that's okay). I'm using sqlite3 and I've heard it can be troublesome when using South to alter database models. Were there any other obvious errors there? I somehow feel there's more I screwed up (such as the order in which I initiated these changes). – Why Not Mar 03 '12 at 02:35
  • It's hard to tell what other danger lurks. I also don't have enough experience with South to be able to guess what you did wrong. Pretty clear that it didn't manage the migration properly. – Carl F. Mar 03 '12 at 02:43