【发布时间】:2021-09-09 12:04:24
【问题描述】:
如果有人能帮助我解决这个 Django 错误,我将不胜感激,我的模型是这样创建的:
from django.db import models
from django.utils.timezone import now
# Create your models here.
class Album(models.Model):
album_title = models.CharField(max_length=200)
creation_date = models.DateTimeField(default=now)
def __str__(self):
return self.album_title
class Photo(models.Model):
name = models.CharField(max_length=300, default="")
album = models.ForeignKey(Album, on_delete=models.CASCADE)
# file = models.ImageField(upload_to=Album.album_title, null=True, blank=True)
description = models.CharField(max_length=1000, default="")
file_url = models.CharField(max_length=1000)
selected = models.BooleanField(default=False)
date_uploaded = models.DateTimeField(default=now)
file_type = models.Choices('Raw', 'JPEG')
def __str__(self):
return str(self.album) + "ID.{:0>3}".format(self.id)
如果我像这样进行迁移,一切正常,但是每当我取消注释该行时:
file = models.ImageField(upload_to=Album.album_title, null=True, blank=True)
在我的班级照片中,我无法进行迁移。
当我运行 python3 manage.py make migrations 代码时,我有这个堆栈跟踪:
Traceback (most recent call last):
File "/Users/adamspierredavid/Documents/django/crueltouch/manage.py", line 22, in <module>
main()
File "/Users/adamspierredavid/Documents/django/crueltouch/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/commands/makemigrations.py", line 190, in handle
self.write_migration_files(changes)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/commands/makemigrations.py", line 227, in write_migration_files
migration_string = writer.as_string()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/writer.py", line 141, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/writer.py", line 99, in serialize
_write(arg_name, arg_value)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/writer.py", line 63, in _write
arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/writer.py", line 271, in serialize
return serializer_factory(value).serialize()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/serializer.py", line 201, in serialize
return self.serialize_deconstructed(path, args, kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/serializer.py", line 88, in serialize_deconstructed
arg_string, arg_imports = serializer_factory(arg).serialize()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/migrations/serializer.py", line 353, in serializer_factory
raise ValueError(
ValueError: Cannot serialize: <django.db.models.query_utils.DeferredAttribute object at 0x10a1bcfd0>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/3.2/topics/migrations/#migration-serializing
我去了这个链接https://docs.djangoproject.com/en/3.2/topics/migrations/#migration-serializing 看看它说什么,但我找不到可以理解的东西(在我的水平上)来解决这个问题。问题是在之前的项目中,当我想添加图像时,Django 要求我安装 Pillow,在我安装之后,我的代码就可以工作了。我可以进行迁移,但现在什么都没有。我使用 Postgres 作为我的数据库。
谢谢你们的帮助。 (P.S:我是学习Django的初学者)
【问题讨论】:
-
你装枕头了吗?
-
是的,我做到了。我在我的代码中发现了错误,它应该是 upload_to="Album.album_title",我忘记了报价,这就是它不起作用的原因。谢谢你的回答
标签: python-3.x django django-models django-rest-framework imagefield