Search code examples
djangoimagekitdjango-imagekit

Imagekit as a foreignkey - adding an image in admin


This relates to adding information in a Foreignkey field in the main admin screen.

I have a Staff model. I need mugshots in 3 formats. I'd like to use django-imagekit for processing.

I've made a Photo model to store the mugshot, and added it as a Foreignkey to Staff as photo = models.ForeignKey(Photo).

Question: How can I add the mugshot directly in the Staff admin screen, i.e. avoid having to upload the mugshots separately in their own admin screen then link to them from Staff?

Thank you for your thoughts ...


Solution

  • Depends on what screen you need it in, but I guess you need it in the details/edit screen? The most simple method is creating a template in your app, called:

    /templates/admin//staff/change_form.html

    In this template place the following:

    {% extends 'admin/change_form.html' %}
    {% block content %}
    {% if not add and original.photo %}
    <img src="{{original.photo.url}}" alt="Photo" />
    {% endif %}
    {{ block.super }}
    {% endblock %}
    

    This places the image above the rest of the content if in edit modus (so no image in add mode).

    If you need it in the list you can add a custom function to your admin.py as well returning the HTML snippet for showing the photo.