开发者

How to access model data when overriding Django admin templates?

sorry if this is an obvious question but I have been searching for a few days and have not been able to come up with a result.

I am creating a simple photo gallery app. There are four galleries, each containing a photo (the photo consists of a 'before' image, 'after' image and caption). I am trying to use django-admin to allow users to click on a gallery and then add photos.

I am using a TabularInline to edit the photos within each gallery. In addition to the default columns on the TabularInline, I would like to add a column that shows a thumbnail preview of the 'before' photo and 'after' photo (I am using easy-thumbnails for this). After much searching, it seems like the best way to do this is to override the django-admin tabularInline.html template and add the column myself - so I created another copy and am trying to edit it now.

What I would like to do is simply reference the Photo object within the Django admin template that I am overriding - but I don't know the appropriate tag to use. I need the reference so I can use it in conjunction with the easy-thumbnails thumbnail tag ... but for the life of me I cannot figure out the template tag that references the object. I have tried iterating through the ModelForm, FormSet, and FieldSet objects but none seem to give me a direct reference to the object.

# models.py

class Gallery(models.Model):
    name = models.CharField(max_length=200)
    url = models.CharField(max_length=200)
    desc = models.TextField()

    def __unicode__(self):
    return self.name

class Photo(models.Model):
    gallery = models.ForeignKey(Gallery)
   开发者_如何转开发 before = models.ImageField(upload_to='gallery')
    after = models.ImageField(upload_to='gallery')
    caption = models.CharField(max_length=1000)
    order = models.IntegerField(blank = True, null = True)

def __unicode__(self):
        return "Photo " + str(self.order)    

# admin.py
class GalleryForm(forms.ModelForm):
model = Gallery
class Media:
    js = (
        '/assets/js/jquery-1.4.2.min.js',
        '/assets/js/jquery-ui-1.8.2.custom.min-admin-sortable.js',
        '/assets/js/menu-sort.js',
    )        

class PhotoInline(admin.TabularInline):
    model = Photo
    extra = 1
    template = "admin/tabular-thumbnails.html"

admin.site.register(Gallery, 
inlines=[PhotoInline],
form = GalleryForm)

Thanks so much in advance and please let me know if there's any additional information I can offer. I am using Django 1.1


In Django 2.1 {{adminform.form.instance.field_name}} worked for me.


{{ form.instance }} will always be the model instance associated with a modelform, assuming there is one.

(Note that ``{{ formset.instance }}` is the instance of the parent model in an inline formset).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜