Python Django AttributeError:&x27;查菲尔德';对象没有属性';型号';迁移后

Python Django AttributeError:&x27;查菲尔德';对象没有属性';型号';迁移后,python,django,django-models,Python,Django,Django Models,注意:我使用的是Django 1.9和Python 3.4.2 当我对其中一个模型进行更改、进行迁移(/manage.py makemigrations)然后尝试应用迁移(/manage.py migrate)时,我遇到了一个奇怪的错误 有一个类似的问题,但没有答案,人们要求更多的信息,在评论中唯一可能的解决方案似乎是删除迁移文件并重新开始-但我真的很想找出是什么导致了这个问题,以及如何正确地修复它 错误: 对我的模型的更改: 变化是我从文章中删除了unique=True,default=tit

注意:我使用的是Django 1.9和Python 3.4.2

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

有一个类似的问题,但没有答案,人们要求更多的信息,在评论中唯一可能的解决方案似乎是删除迁移文件并重新开始-但我真的很想找出是什么导致了这个问题,以及如何正确地修复它

错误: 对我的模型的更改: 变化是我从文章中删除了
unique=True,default=title
,并添加了
blank=False

我的迁移文件: 这将
slug
字段的默认值设置为
CharField
实例。这是不对的,我很惊讶这没有给你一个错误


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

这很奇怪,因为原始代码是无效的:您不能引用其他类似的字段作为默认字段。所以我不确定这是怎么回事。是的,我也不明白,因为某种原因,它一直工作到现在。我更改了我的初始迁移文件(
0001_initial.py
),并将默认值从
CharField
更改为
None
,比如:
('slug',models.SlugField(默认值=无,最大长度=255,唯一值=真))
。这就成功了!现在,我的迁移正在运行,没有错误。谢谢,谢谢。。。我被同样的错误消息弄糊涂了,这指出了基本相同的原因:CharField默认值设置为CharField实例。
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)
# -*- 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),
        ),
    ]
slug = models.SlugField(max_length=255, unique=True, default=title)