Search code examples
djangoadminimagefield

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

  • [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
            ))