一、举例

tip/tip.js
        var react = function () {
        if (django.jQuery('#id_tiptype').val() == 'content') {
            django.jQuery('#id_content').parent().parent().show(500);
            django.jQuery('#id_image').parent().parent().hide(500);
            django.jQuery('#id_cropping').parent().parent().hide(500);
        }
        else {
            django.jQuery('#id_content').parent().parent().hide(500);
            django.jQuery('#id_image').parent().parent().show(500);
            django.jQuery('#id_cropping').parent().parent().show(500);
        }
        };

        #当选择的类型改变的时候触发react函数
        django.jQuery(function () {
            react();
            django.jQuery('#id_tiptype').on('change', react);
        });

二、admin.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from image_cropping import ImageCroppingMixin

from salmonella.admin import SalmonellaMixin

from tip.models import Tip,TipTag


@admin.register(Tip)
class TipAdmin(ImageCroppingMixin, SalmonellaMixin, admin.ModelAdmin):
    # search_fields = ('tiptype',)
    list_filter = ('enabled',)
    list_display = ('tiptype', 'enabled', 'get_tag','image')
    salmonella_fields = ('tags', )



    # fields = ('tiptype', 'enabled','tags','image')

    def get_tag(self, obj):
        print(obj.tags.all())
        if obj.tags.all():
            tag_list = [i.name for i in obj.tags.all()]
            return ','.join(tag_list)
        else:
            return ''

    get_tag.short_description = '小贴士标签'

    class Media():
        js = ('tip/tip.js',)


@admin.register(TipTag)
class TiptagAdmin(admin.ModelAdmin):
    search_fields = ('name', )
    list_display = ("name", )

 

posted on 2018-05-16 16:02  海燕。  阅读(2152)  评论(0编辑  收藏  举报