Django AttributeError:'CharField'对象在迁移后没有属性'model'

时间:2016-01-03 13:23:32

标签: python django django-models

注意:我正在使用Django 1.9和Python 3.4.2

当我对其中一个模型进行更改,进行迁移(./manage.py makemigrations)然后尝试应用迁移(./manage.py migrate)时,我收到一个奇怪的错误。

有一个类似的问题here,但没有答案,人们要求提供更多信息,评论中唯一可能的解决方案似乎是删除迁移文件并重新开始 - 但我真的很喜欢找出导致此问题的原因以及如何正确解决问题。

错误:

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'

改变前的模型:

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

对我的模型的更改:

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

更改是我从文章中删除了unique=True, default=title并添加了blank=False

我的迁移文件:

# -*- 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),
        ),
    ]

1 个答案:

答案 0 :(得分:4)

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

这会将slug字段的默认值设置为 CharField实例。这不正确,我很惊讶这并没有在早些时候给你一个错误。

我认为唯一的解决方案是更改引入旧默认值的迁移文件,并将默认值更改为有效值。