Django AttributeError: 'CharField' object has no attribute 'model' after migration
Question: **Note: I'm using Django 1.9 and Python 3.4.2**

I'm getting a strange error when I make a change to one of my models, make a
migration (`./manage.py makemigrations`) and then try to apply the migration
(`./manage.py migrate`).

There is a similar question
[here](http://stackoverflow.com/questions/28743021/attributeerror-charfield-
object-has-no-attribute-model), but there is no answer, people are asking for
more information and the only possible solution in the comments seems to be
delete the migration files and start again - but I would really like to find
out what is causing this issue and how to fix it properly.

### The error:

    
    
    Operations to perform:
      Apply all migrations: contenttypes, admin, auth, blog, sessions
    Running migrations:
      Rendering model states... DONE
      Applying blog.0004_auto_20160103_1321...Traceback (most recent call last):
      File "./manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
        utility.execute()
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
        output = self.handle(*args, **options)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
        executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
        self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
        state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
        state = migration.apply(state, schema_editor)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/migrations/migration.py", line 123, in apply
        operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
        schema_editor.alter_field(from_model, from_field, to_field)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 482, in alter_field
        old_db_params, new_db_params, strict)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 564, in _alter_field
        old_default = self.effective_default(old_field)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 210, in effective_default
        default = field.get_db_prep_save(default, self.connection)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
        prepared=False)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 720, in get_db_prep_value
        value = self.get_prep_value(value)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1112, in get_prep_value
        return self.to_python(value)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1108, in to_python
        return smart_text(value)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/utils/encoding.py", line 42, in smart_text
        return force_text(s, encoding, strings_only, errors)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/utils/encoding.py", line 76, in force_text
        s = six.text_type(s)
      File "/Users/owen/src/projects/owen/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 188, in __str__
        model = self.model
    AttributeError: 'CharField' object has no attribute 'model'
    

### My models before the change:

    
    
    from django.db import models
    from django.utils import timezone
    
    
    class Category(models.Model):
        title = models.CharField(max_length=100, unique=True)
    
        def __str__(self):
            return self.title
    
        def number_of_articles(self):
            articles = Article.objects.filter(categories__pk=self.id)
            return articles.count()
    
    
    class Article(models.Model):
        title = models.CharField(max_length=255, unique=True)
        slug = models.SlugField(max_length=255, unique=True, default=title)
        pub_date = models.DateTimeField('date published', default=timezone.now)
        intro_text = models.TextField(blank=True)
        body_copy = models.TextField(blank=False)
        votes_helpful = models.IntegerField(default=0, blank=True)
        votes_unhelpful = models.IntegerField(default=0, blank=True)
    
        categories = models.ManyToManyField(Category, blank=True)
    
        class Meta:
            ordering = ('-pub_date',)
    
        def __str__(self):
            return self.title
    
        def has_votes(self):
            total_num_votes = self.votes_helpful + self.votes_unhelpful
            return total_num_votes > 0
    

### The change to my model:

    
    
    slug = models.SlugField(max_length=255, blank=True)
    

The change is that I have removed `unique=True, default=title` from
**Article** and added `blank=False`.

### My migration file:

    
    
    # -*- coding: utf-8 -*-
    # Generated by Django 1.9 on 2016-01-03 13:21
    from __future__ import unicode_literals
    
    from django.db import migrations, models
    
    
    class Migration(migrations.Migration):
    
        dependencies = [
            ('blog', '0003_auto_20151230_1643'),
        ]
    
        operations = [
            migrations.AlterField(
                model_name='article',
                name='slug',
                field=models.SlugField(blank=True, max_length=255),
            ),
        ]
    


Answer: 
    slug = models.SlugField(max_length=255, unique=True, default=title)
    

This sets the default for your `slug` field to _a`CharField` instance_. That's
not correct, I'm surprised this didn't give you an error earlier on.

I think the only solution is to change the migration file that introduced the
the old default, and change the default to a valid value.

