我得到了AttributeError:' FileField'对象没有属性' model'每当我运行python manage.py makemigrations和python manage.py migrate 当我运行迁移时,请参阅下面的model.py和我的上一个迁移文件
迁移:0014_slider_slider title
class Migration(migrations.Migration):
dependencies = [
('shops', '0013_auto_20170219_2049'),
]
operations = [
migrations.AddField(
model_name='slider',
name='Slider Title',
field=models.CharField(default=models.FileField(blank=True, null=True, upload_to=b''), max_length=20),
),
]
迁移的输出
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, shops
Running migrations:
Applying shops.0014_slider_slider title...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 359, 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 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/schema.py", line 43, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 395, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 147, in column_sql
default_value = self.effective_default(field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 221, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 755, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 747, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1081, in get_prep_value
return self.to_python(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1077, in to_python
return smart_text(value)
File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 41, in smart_text
return force_text(s, encoding, strings_only, errors)
File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 78, in force_text
s = six.text_type(s)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 188, in __str__
model = self.model
AttributeError: 'FileField' object has no attribute 'model'
models.py
from __future__ import unicode_literals
from django.db import models
#Create your models here.
class Slider(models.Model):
slider = models.FileField()
slider_title = models.CharField(max_length=20)
def __str__(self):
return self.slider_title
class ShopCategories(models.Model):
category = models.CharField(max_length=50, unique=True)
def __str__(self):
return self.category
class NewShop(models.Model):
category = models.ForeignKey(ShopCategories)
main_image = models.FileField()
name = models.CharField(max_length=100, unique=True)
tagline = models.CharField(max_length=50, default='Enter tagline here2')
description = models.TextField(default='enter shop description')
shop_image = models.FileField()
def __str__(self):
return self.name
答案 0 :(得分:0)
因为你拥有的大多数FileField对象都是图像,所以Django的ImageField比Filefield更好。查看docs
答案 1 :(得分:0)
出于某种原因,您已经指定了另一个字段类型作为charfield的默认值,我怀疑它是否会起作用,只需将其更改为具有更好的char字段默认值
operations = [
migrations.AddField(
model_name='slider',
name='Slider Title',
field=models.CharField(default='', max_length=20),
),
]