AttributeError:'FileField'对象没有属性'model'(AttributeError: 'FileField' ob

编程入门 行业动态 更新时间:2024-10-25 01:26:06
AttributeError:'FileField'对象没有属性'model'(AttributeError: 'FileField' object has no attribute 'model')

我得到AttributeError:'FileField'对象没有属性'模型'每当我运行python manage.py makemigrations和python manage.py migrate请参阅下面的model.py和我上次迁移文件,当我运行迁移

迁移:0014_slider_slider标题

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

I get AttributeError: 'FileField' object has no attribute 'model' whenever i run python manage.py makemigrations and python manage.py migrate Please see below model.py and my last migration file when i run migrate

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

Output of Migrate

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

最满意答案

出于某种原因,你已经指定了另一个Field类型作为charfield的默认值,我怀疑它是否会工作,只需将其更改为char字段的更好默认值

operations = [ migrations.AddField( model_name='slider', name='Slider Title', field=models.CharField(default='', max_length=20), ), ]

For some reason you've specified another Field type as a default for a charfield which I doubt would ever work, just change this to have a better default for a char field

operations = [ migrations.AddField( model_name='slider', name='Slider Title', field=models.CharField(default='', max_length=20), ), ]

更多推荐

本文发布于:2023-07-26 03:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   FileField   AttributeError   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!