1

I have something like the following:

class Destination(models.Model):
    name = models.CharField
    picture = models.ImageField

    def __unicode__(self):
        return u"%s" % self.name

class Vacation(models.Model):
    destination = models.ForeignKey(Destination)

When creating the model in my Django Admin interface, I'd like my Destinations to be displayed as radio buttons with the Destination name and Destination picture.

I'm using a custom add_form template so displaying radio buttons with destination name is no problem, but including the picture is difficult.

I would like to leave __unicode__(self) as-is, I only need the picture returned with the object in this admin view. Also I don't want to inline the object.

Any advice on how to do this (including how to incorporate it into the template) would be great!

EDIT: This SO post comes very close to what I need, but I would like to access the individual choice data instead of parsing it from a modified label.

Community
  • 1
  • 1
grokpot
  • 1,462
  • 20
  • 26

1 Answers1

0

This is not an admin-specific answer, but I think it should work in admin if you can use a custom template for the form.

You could make a modified widget (probably as a subclass of an existing Django widget), that send extra fields from the model to a custom widget template.

You could also render the form manually in the template where it's displayed, and make an inclusion tag that fetches any extra information using the id of your destination object, which is passed as the value of the option.

For example:

your_template.html

{% load destinations %}
{% for opt in form.destination %}
{{ opt.tag }}
{% destination opt.data.value %}
{% endfor %}

destinations.py (in your_app/templatetags)

from django import template
from your_app.models import Destination

register = template.Library()

@register.inclusion_tag('your_app/destination-option.html')
def destination(id):
    destination=Destination.objects.filter(id=int(id)).first()
    return {'destination':destination}

destination-option.html

<!-- any formatting you prefer -->
{{destination.title}}
<img src="{{destination.picture.image_url}}">
pereric
  • 109
  • 3