简体   繁体   中英

Django-Admin custom ImageField

I wanna add additional ImageField which obtains image from static folder to the model. I've tried to use some answers from the same questions, but there aren't anything about ImageField .

[SOLUTION]

I've found some way to load image via 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
        ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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