【问题标题】:AttributeError: 'CharField' object has no attribute 'model'AttributeError:“CharField”对象没有属性“模型”
【发布时间】:2015-04-28 20:43:35
【问题描述】:
#models.py file

from django.db import models


class Stocks(models.Model):
    symbol = models.CharField(max_length=20, unique=True)
    text = models.CharField(max_length=50)



class DataChoice(models.Model):
    choice_text = models.CharField(max_length=20)

    def __unicode__(self):
        return self.choice_text

我更改了模型文件并在“股票”中添加了“文本”。我运行了 makemigrations 命令,然后运行了 migrate 命令,但是当我运行 migrate 命令时出现错误,不知道为什么。

这是完整的错误回溯:

Operations to perform:
  Apply all migrations: admin, contenttypes, stocks, auth, sessions
Running migrations:
  Applying stocks.0002_auto_20150226_1624...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
field,
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 167, in add_field
self._remake_table(model, create_fields=[field])
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 74, in _remake_table
self.effective_default(field)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 171, in effective_default
default = field.get_default()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 720, in get_default
return force_text(self.default, strings_only=True)
 File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 85, in force_text
s = six.text_type(s)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 172, in __str__
model = self.model
AttributeError: 'CharField' object has no attribute 'model'

迁移文件:

from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('stocks', '0005_auto_20150226_1714'),
    ]

    operations = [
        migrations.AlterField(
            model_name='stocks',
            name='text',
            field=models.CharField(max_length=50),
            preserve_default=True,
        ),
    ]

【问题讨论】:

  • 请显示完整错误回溯。
  • @DanielRoseman 我已经添加了,请查看。
  • 您能发布您的迁移文件吗?错误可能会在那里
  • @awwester 你的意思是迁移文件夹中的初始文件,对吧?
  • 你上面提供的迁移文件是stocks.0002_auto_20150226_1624 ?奇怪的是,这似乎取决于未来在同一个应用程序中的迁移:('stocks', '0005_auto_20150226_1714') ..您能否澄清一下您的迁移结构,之前的字段名称是什么,还有什么变化?

标签: python django django-models


【解决方案1】:

可能是添加一个默认值或null=True,可能会解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2021-10-20
    • 2019-05-11
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多