pythondjangodjango-modelsdjango-profiles

Adding ImageField to Django Models Results in Error


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.


Solution

  • 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.