如果有人可以帮助我解决这个 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/stable/topics/migrations/#migration-serializing
我去了这个链接https://docs.djangoproject.com/en/3.2/topics/migrations/#migration-serializing看看它说了什么,但我找不到可以理解的东西(在我的水平)来修复的问题。问题是,在之前的项目中,当我想添加图像时,Django 要求我安装 Pillow,在我安装之后,我的代码就可以工作了。我可以进行迁移,但现在什么也做不了。我使用 Postgres 作为我的数据库。
谢谢你们的帮助。 (P.S:我是学习Django的初学者)
这里的问题是
ImageField
,它有一个字段upload_to
,它应该是一个路径名,它不能指向一个尚未创建实例的类Album
。如果您的 Photo
应根据 album
字段保存,我想会看到
代码行file = models.ImageField(upload_to=Album.album_title, null=True, blank=True)
和行album = models.ForeignKey(Album, on_delete=models.CASCADE)
,我在这里认为,你想根据你指向的file
保存album
。
你可以尝试这样的事情
def get_path(instance, filename):
return '{0}/{1}'.format(instance.file.album_title, filename)
你的
file
行代码应该看起来像
file = models.ImageField(upload_to=get_path, null=True, blank=True)
您在这里所做的是,定义一个函数,确保根据您的输入动态设置您的路径。在函数内部
get_upload_path
您可以根据需要更改路径结构。
欲了解更多见解,您可以参考此文档 https://docs.djangoproject.com/en/stable/ref/models/fields/#django.db.models.FileField.upload_to
Imagefield
继承自django模型中的FileField
,所以这个文档就足够了。