samedi 25 juin 2016

Exception Type: OperationalError - Exception Value: no such column: blog_article.slug


I am making a blog using django. I created an app named blog, and in models.py I created the following model, for articles on this blog :

class Article(models.Model):
    title = models.CharField(max_length = 100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    author = models.CharField(max_length = 100)
    COUNTRIES = (
        ("fr", "france"),
        ("ge", "germany"),
        )
    nationality = models.CharField(choices= COUNTRIES, max_length = 20)
    publication_date = models.DateTimeField()
    content = models.TextField()

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_article', None, { 'slug': self.slug })

In admin.py I did the following, in order to have the slug prepopulated with the title :

class ArticleAdmin(admin.ModelAdmin):
    exclude = ['posted']
    prepopulated_fields = {'slug': ('title',)}

I have ran python manage.py makemigrations and migrate several times, tried to squash the migrations as I found in other posts here. When I run makemigrations I get : "No changes detected". When I run migrate I get "No migrations to apply".

However, when I runserver and go to the admin interface in order to add a new article, fill the "title" Field, see the slug field filled automatically (due to prepopulation) and want to SAVE, here is what I get:

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/blog/article/add/

Django Version: 1.9.4
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'blog.apps.BlogConfig']
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.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  541.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  244.             return view(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in add_view
  1437.         return self.changeform_view(request, None, form_url, extra_context)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in inner
  184.                     return func(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1370.             if form.is_valid():

File "/Library/Python/2.7/site-packages/django/forms/forms.py" in is_valid
  161.         return self.is_bound and not self.errors

File "/Library/Python/2.7/site-packages/django/forms/forms.py" in errors
  153.             self.full_clean()

File "/Library/Python/2.7/site-packages/django/forms/forms.py" in full_clean
  364.         self._post_clean()

File "/Library/Python/2.7/site-packages/django/forms/models.py" in _post_clean
  402.             self.validate_unique()

File "/Library/Python/2.7/site-packages/django/forms/models.py" in validate_unique
  411.             self.instance.validate_unique(exclude=exclude)

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in validate_unique
  922.         errors = self._perform_unique_checks(unique_checks)

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in _perform_unique_checks
  1017.             if qs.exists():

File "/Library/Python/2.7/site-packages/django/db/models/query.py" in exists
  651.             return self.query.has_results(using=self.db)

File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in has_results
  501.         return compiler.has_results()

File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in has_results
  819.         return bool(self.execute_sql(SINGLE))

File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  848.             cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/utils.py" in __exit__
  95.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  323.         return Database.Cursor.execute(self, query, params)

**Exception Type: OperationalError at /admin/blog/article/add/
Exception Value: no such column: blog_article.slug**

I get the same result if I click on "Article" in the admin interface. I tried to squash the migrations, to makemigrations and migrate again, but it hasn't worked... I looked everywhere and did not find an answer that worked for me -- thank you so much if you can give some tips on this !


Aucun commentaire:

Enregistrer un commentaire