【发布时间】:2021-12-04 02:32:33
【问题描述】:
问题:我在模型中有一个 ImageField。 TestCase 无法找到默认图像文件来对其执行图像调整大小(@receiver(pre_save, sender=UserProfile) 装饰器)
class UserProfile(Model):
user = ForeignKey(AUTH_USER_MODEL ...)
...
photo = ImageField(
verbose_name=_("Photo"),
upload_to="media",
null=True,
blank=True,
help_text=_("a photo will spark recognition from others."),
default="default.png")
概述:我正在本地运行从 pycharm 到 docker 容器的测试。该项目基于 django cookiecutter 项目。
我在presave钩子中搜索了目标文件,在这2个目录中找到了该文件['/app/media/default.png', '/opt/project/media/default.png']
我在 '/app/
如何让媒体文件在这些目录中查找?
@receiver(pre_save, sender=UserProfile)
def user_profile_face_photo_reduction(sender, instance, *args, **kwargs):
"""
This creates the thumbnail photo for a concept
"""
test_image_size(instance) # this is the function that gives problems. it's below.
im = generic_resize_image(size=face_dimensions, image=instance.photo)
save_images_as_filename(
filename=Path(instance.photo.path).with_suffix(".png"),
image=im,
instance=instance,
)
def test_image_size(instance=None, size_limit=None):
"""
size_limit is a namedtuple with a height and width that is too large to save.
instance needs to have a photo attribute to work
"""
if instance and instance.photo:
print([x.absolute() for x in sorted(Path("/").rglob("default.png"))]) # this is where I got the actual location info
assert instance.photo.size <= 10_000_000, ValueError("Image size is too big") # THE PROBLEM LINE IS THIS ONE.
if (
instance.photo.width > size_limit.width
or instance.photo.height > size_limit.height
):
raise ValueError(_("the picture dimensions are too big"))
【问题讨论】:
标签: django docker cookiecutter-django