使用具有动态上传路径的 FileField Django 迁移失败
Django migration fails using FileField with dynamic upload path
我正在尝试为 django FileFiled 使用动态上传路径。
这是我的模型:
def use_assignment_path(instance, filename):
return 'assignment/%s/%s' % (instance.name, filename)
class Assignment(models.Model):
admin = models.ForeignKey(Admin)
name = models.CharField(max_length=50, unique=True)
lang = models.CharField(max_length=5, default='c', choices=(('c', 'c'), ('java', 'java')))
pointsRecommended = models.IntegerField()
file0 = models.FileField(upload_to=use_assignment_path)
file1 = models.FileField(upload_to=use_assignment_path, default='', blank=True)
当我尝试迁移模型时出现以下错误:
Traceback (most recent call last):
File "manage.py", line 10, in <module> execute_from_command_line(sys.argv)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 83, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 52, in __init__
self.build_graph()
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 203, in build_graph
self.load_disk()
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 114, in load_disk
migration_module = import_module("%s.%s" % (module_name, migration_name))
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py", line 11, in <module>
class Migration(migrations.Migration):
File "/media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py", line 23, in Migration
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
AttributeError: module 'models' has no attribute 'AutoField'
如果不知道为什么会发生,当我使用静态上传路径时它工作得很好。
我已经阅读了几篇有类似问题的帖子,但在知道之前没有任何帮助。
坦克你!
删除“/media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py”文件并重试
您的应用名称 models
似乎与 django.db.models
冲突。尝试重命名您的应用程序,删除初始迁移,然后重新运行 makemigrations
和 migrate
.
我正在尝试为 django FileFiled 使用动态上传路径。 这是我的模型:
def use_assignment_path(instance, filename):
return 'assignment/%s/%s' % (instance.name, filename)
class Assignment(models.Model):
admin = models.ForeignKey(Admin)
name = models.CharField(max_length=50, unique=True)
lang = models.CharField(max_length=5, default='c', choices=(('c', 'c'), ('java', 'java')))
pointsRecommended = models.IntegerField()
file0 = models.FileField(upload_to=use_assignment_path)
file1 = models.FileField(upload_to=use_assignment_path, default='', blank=True)
当我尝试迁移模型时出现以下错误:
Traceback (most recent call last):
File "manage.py", line 10, in <module> execute_from_command_line(sys.argv)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/mb/.local/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 83, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 52, in __init__
self.build_graph()
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 203, in build_graph
self.load_disk()
File "/home/mb/.local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 114, in load_disk
migration_module = import_module("%s.%s" % (module_name, migration_name))
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py", line 11, in <module>
class Migration(migrations.Migration):
File "/media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py", line 23, in Migration
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
AttributeError: module 'models' has no attribute 'AutoField'
如果不知道为什么会发生,当我使用静态上传路径时它工作得很好。
我已经阅读了几篇有类似问题的帖子,但在知道之前没有任何帮助。
坦克你!
删除“/media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py”文件并重试
您的应用名称 models
似乎与 django.db.models
冲突。尝试重命名您的应用程序,删除初始迁移,然后重新运行 makemigrations
和 migrate
.