[Django]-Django admin show field only if selectbox has a particular value

3👍

I solved that using jquery and adding a class Media on my ModelAdmin

admin.py

class FixedIncomeAdmin(admin.ModelAdmin):
model = FixedIncome
can_delete = False
list_display = ['name', 'country', 'industry', 'isin', 'coupon', 'payment_frequency']
list_filter = ['name', 'isin', 'country', 'industry']
search_fields = ['name', 'isin', 'country', 'industry']

class Media:
    js = ('/static/admin/js/assets_admin.js',)

assets_admin.js

django.jQuery('#id_payment_frequency').change(function(){
if(django.jQuery("#id_payment_frequency option:selected").text() == 0)
    {
        django.jQuery(".form-row.field-months").hide();
    }else
    {
        django.jQuery(".form-row.field-months").show();
    }
});

if the selectbox of “payment_frequency” (0 to 12) has a 0, then months is hidden in the form

Hope to help to someone.! 🙂

Leave a comment