问题:我在模型中有一个ImageField。TestCase无法找到默认的图像文件来对其执行图像大小调整(一个@receiver(pre_save,sender=UserProfile)decorator之类的东西)
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项目。
我在预售挂钩中搜索了目标文件,在这两个目录中找到了该文件[“/app/media/default.png”,“/opt/project/media/default.png'”]
我在'/app/<my_project_name>/media/default.png'
如何在这些目录中查找媒体文件?
@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"))