如何以缩略图的形式显示select(manytomany)?

时间:2015-11-12 09:51:05

标签: django django-admin

我有模特:

int

我希望在名称缩略图中显示多选(在管理员的编辑产品页面上)。怎么做?

1 个答案:

答案 0 :(得分:0)

我的解决方法,在admin:

class ProductForm(forms.ModelForm):

    class Meta:
        model = Product

    def __init__(self, *args, **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs)

        images = ProductImage.objects.all()
        items = []
        for image in images:
            items.append(
                (image.pk, mark_safe('%s' % image.image.url))
            )
        self.fields['images'].choices = items

class ProductAdmin(ModelAdmin):
    form = ProductForm

site.register(Product, ProductAdmin)

并覆盖base.html:

jQuery(function(){
    jQuery('#id_images').css('width', '400px').css('height', '400px');
    jQuery('#id_images option').each(function(){
        jQuery(this).attr('style', 'background: url('+jQuery(this).text()+') no-repeat; background-size: 70px 70px; height: 80px; padding-left: 80px; line-height: 80px;');
    });
});