我正在尝试使用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'
如果不知道为什么会发生这种情况,当我使用静态上传路径时,它的效果非常好。
我已经阅读了几篇有类似问题的帖子但直到知道才有帮助。
坦克你!
答案 0 :(得分:0)
删除&#34; /media/sf_Websites/HM/trunk/assignments/models/migrations/0001_initial.py"文件,然后再试一次
答案 1 :(得分:0)
您的应用名称models
似乎与django.db.models
发生冲突。尝试重命名您的应用,删除初始迁移,然后重新运行makemigrations
和migrate
。