AttributeError:“ CharField”对象没有属性“ model” [英] AttributeError: 'CharField' object has no attribute 'model'

查看:246
本文介绍了AttributeError:“ CharField”对象没有属性“ model”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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命令时出现错误,不知道为什么。

I have changed models file and added "text" inside "Stocks". I ran makemigrations command and then migrate command but when I run migrate command I got an error, don't know why.

这是完整的错误回溯:

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


推荐答案

可能添加默认值或 null = True ,可能会解决此问题。

May be adding a default value or null=True, may solve this issue.

这篇关于AttributeError:“ CharField”对象没有属性“ model”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆