daniyalzade / django_reverse_admin

Django Reverse Admin
BSD 3-Clause "New" or "Revised" License
199 stars 54 forks source link

custom_field #291

Closed Rishab08 closed 2 years ago

Rishab08 commented 2 years ago

how to give custom fields with resolver function. like image preview

Rishab08 commented 2 years ago

In the source code I discovered that it is possible to specify an admin_class for the reverse inline:

class FooAdmin(ReverseModelAdmin):
    inline_type = 'stacked'
    inline_reverse = [
        {
            'field_name': 'bar',
            'admin_class': BarInline
        }
    ]

And then in the BarInline:

class BarInline(admin.StackedInline):
    model = Bar
readonly_fields = ["brand_logo_preview", "brand_image_preview"]
    fieldsets = (
        (None, {
            'fields': (
                ('brand_logo_preview'),
                ('brand_image_preview'),

          )
        }),)

    def brand_logo_preview(self, obj):
        return format_html('<img src="{}" width="100" height="100" />'.format(obj.brand_logo.url))

    brand_logo_preview.short_description = "logo preview"

    def brand_image_preview(self, obj):
        return format_html('<img src="{}" width="100" height="100" />'.format(obj.brand_image.url))

    def has_delete_permission(self, request, obj=None):
        return False

and hence we can add a preview