Search code examples
pythondjangoformsdjango-adminfield

React on form changes


I have a general question to the django-admin.

Is it possible to react on form changes?

I have a select field in my django-admin detail site. Whenever I change the data from the select field, I want to change fields which are read-only.

Has anybody ever dealt with this issue?


Solution

  • My two cents:

    As any other guys said it is a javascript work. In admin pages Django pases jquery. It is called django.jQuery. So basilly you would do what @Ashfaq suggested. You will create a custom_script.js and added to the Media metaclass.

    Basically(as @Ashfaq):

    class MyModelAdmin(admin.ModelAdmin):
        class Media:
            js = ("js/custom_script.js",)
    

    and custom_script.js will be something like this(assuming that your select field is called id_category):

    django.jQuery( document ).ready(function() {
        console.log( "ready!" );
        django.jQuery('#id_category').change(function() {
          alert( "Handler for #id_category was called." );
        });
    });
    

    The ready function will guarantee the handler is getting set.