Best way to add an image from a filefield to django 2.0 admin?

16 views
Skip to first unread message

Mark Phillips

unread,
Jun 4, 2018, 9:02:36 PM6/4/18
to django users
I have a Document model that uploads an image. I have a DocumentMetaData model with a JSON field where I "crud" metadata for the documents in the Document model. 

I would like to add the image of the document in the Document model to the change form of the DocumentMetaData model in the admin. How do I do it? Do I have to override the admin/change_form.html for the DocumentMetaData model?

Thanks!

Mark

Daniel Germano Travieso

unread,
Jun 6, 2018, 12:18:43 PM6/6/18
to django...@googlegroups.com
Hello Mark.

You can just override the Model Admin for your model and insert an Inline Form for example.

Check the documentation for customizing the model admin!

Hope it helps!


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEqej2PYNudeSWa5CY934m8kMWO%3Dhcpj4%3DxQud9C-wZCHoaS4g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Mark Phillips

unread,
Jun 6, 2018, 7:12:49 PM6/6/18
to django users
Daniel,

I did read the docs and could not get it to work. Probably operator error, as I went back a day later and followed the docs, and it worked as advertised! 

In the interest of completeness, here are the snippets of what I did:

class DocumentMetaDataAdmin(admin.ModelAdmin):
    readonly_fields = ('get_image_2', )
    
    fieldsets = (
        (None, {
            'fields': ('get_image_2', ),
            'classes': ('extrapretty', ),
        }),
    )

    def get_image_2(self, obj):
        image = ""   # need to add a default image
        if (obj.document_id.storage_file_name):
            image = '<a href="{image_link}"><img src="{thumb_nail}" style="height:400px"/></a>'.format(image_link = obj.document_id.storage_file_name.url, thumb_nail = obj.document_id.storage_file_name.url)
        return format_html(image)

Thanks!

Mark

PS Still working on defining the 'extrapretty' CSS!

On Wed, Jun 6, 2018 at 5:17 AM, Daniel Germano Travieso <danielg...@gmail.com> wrote:
Hello Mark.

You can just override the Model Admin for your model and insert an Inline Form for example.

Check the documentation for customizing the model admin!

Hope it helps!


On Mon, Jun 4, 2018, 18:02 Mark Phillips <ma...@phillipsmarketing.biz> wrote:
I have a Document model that uploads an image. I have a DocumentMetaData model with a JSON field where I "crud" metadata for the documents in the Document model. 

I would like to add the image of the document in the Document model to the change form of the DocumentMetaData model in the admin. How do I do it? Do I have to override the admin/change_form.html for the DocumentMetaData model?

Thanks!

Mark

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages