Reset Field In Django Admin Page

Imagine, that I have model and in the Django admin form I can update my fields. I want to implement something like: update one field and the second one will be reset in admin form in real time (I'll hope it can help real admin in the future do not forget redefine this second field, because it's very important in my situation). Is that possible to implement something like that without custom admin form?



To achieve this without a custom template or form, you can just include a custom script, for example:

Assuming you have an app named people and it has a model called Person with two fields first_name and last_name:

Include javascript into the admin page

# people/

from django.contrib import admin
from people.models import Person

class PersonAdmin(admin.ModelAdmin):
    class Media:
        js = ('people/js/person.js',), PersonAdmin)

Then create the person/js/person.js script:

'use strict';

//execute when everything is loaded
window.addEventListener('load', () => {
    let first_field = document.querySelector('form input[name=first_name]'); //field name
    let second_field = document.querySelector('form input[name=last_name]'); //field name

    //just when the fields are found (add or update)
    if(first_field && second_field) {
        //here you can implement whatever logic you want
        first_field.addEventListener('input', () => {
            second_field.value = '';

Now every time the first_name changes last_name will be cleared. This idea can be extended to do more interesting things.