Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Admin - on form change

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 readonly.

Anybody ever dealed with this issue?

Thanks and Greetings

like image 830
Creative crypter Avatar asked Nov 22 '18 19:11

Creative crypter


People also ask

How to change ‘Django administration’ text?

How to change ‘Django administration’ text? ¶ By default Django admin shows ‘Django administration’. You have been asked to replace this with ‘UMSRA Administration’ The text is at these pages: 1.1. Login, Listview and Changeview Page ¶ By default it looks like this and is set to “Django administration” site_header can be set to change this. 1.2.

What is the modeladmin class in Django?

The ModelAdmin class is the representation of a model in the admin interface. Usually, these are stored in a file named admin.py in your application. Let’s take a look at an example of the ModelAdmin: from django.contrib import admin from myapp.models import Author class AuthorAdmin(admin.ModelAdmin): pass admin.site.register(Author, AuthorAdmin)

What is a form in Django?

In the context of a web application, ‘form’ might refer to that HTML <form>, or to the Django Form that produces it, or to the structured data returned when it is submitted, or to the end-to-end working collection of these parts. At the heart of this system of components is Django’s Form class.

What is the default admin site in Django?

A Django administrative site is represented by an instance of django.contrib.admin.sites.AdminSite; by default, an instance of this class is created as django.contrib.admin.site and you can register your models and ModelAdmin instances with it. If you want to customize the default admin site, you can override it.


2 Answers

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.

like image 75
edilio Avatar answered Sep 30 '22 12:09

edilio


I think the thing that will work here is to add jQuery + your custom javascript and play with the events / clicks what-ever you want with elements.

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

in custom_script you can add click or change events as you want.

like image 22
Ashfaque Ali Solangi Avatar answered Sep 30 '22 13:09

Ashfaque Ali Solangi