简体   繁体   English

Django-Admin 自定义 ImageField

[英]Django-Admin custom ImageField

I wanna add additional ImageField which obtains image from static folder to the model.我想添加从static文件夹中获取图像的附加ImageField到 model。 I've tried to use some answers from the same questions, but there aren't anything about ImageField .我尝试使用相同问题的一些答案,但没有任何关于ImageField的内容。

[SOLUTION] [解决方案]

I've found some way to load image via def我找到了一些通过def加载图像的方法


# admin.py

@admin.register(Course)
class CourseAdmin(..., admin.ModelAdmin):
...
readonly_fields = [..., 'headshot_certic_preview_image', ]
fieldsets = (
... ,
('Image info', {
            'fields': ('image', ('image_courses_and_course', 'image_dash'),
                       ('certic_preview_image', 'first_row_course_name', 'second_row_course_name',
                        'headshot_certic_preview_image'),
                       ),
        }),
...
     )
def headshot_certic_preview_image(self, obj):
        return mark_safe('<img src="{url}" width="{width}" height={height} />'.format(
            url=os.path.join('media/', obj.certic_preview_image.url),
            width=700,
            height=516
        ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM