AttributeError:'CharField'对象没有属性'model'

时间:2022-04-15 18:17:34
#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

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.

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

This is full error traceback:

这是完整的错误追溯:

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'

migrations file:

迁移文件:

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

1 个解决方案

#1


0  

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

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

#1


0  

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

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