Skip to content

Fix inline forms for django 4.1+ #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

guilleijo
Copy link

@guilleijo guilleijo commented Feb 10, 2023

Django 4.1 changed the way the formset:added and formset:removed are called. $row and formsetName are no longer available, they need to be used from the event param.

When using a better array field in an inline admin the Add another button does not work on new items. The Uncaught TypeError: Cannot read properties of undefined (reading '0') error is seen in the console when adding a new inline item because $row is undefined.

From the django JavaScript customizations in the admin docs:

Changed in Django 4.1:
In older versions, the event was a jQuery event with $row and formsetName parameters. It is now a JavaScript CustomEvent with parameters set in event.detail.

See the Supporting versions of Django older than 4.1 section at the bottom of the page.

If your event listener still has to support older versions of Django you have to use jQuery to register your event listener. jQuery handles JavaScript events but the reverse isn’t true.

You could check for the presence of event.detail.formsetName and fall back to the old listener signature as follows:

function handleFormsetAdded(row, formsetName) {
    // Do something
}

$(document).on('formset:added', (event, $row, formsetName) => {
    if (event.detail && event.detail.formsetName) {
        // Django >= 4.1
        handleFormsetAdded(event.target, event.detail.formsetName)
    } else {
        // Django < 4.1, use $row and formsetName
        handleFormsetAdded($row.get(0), formsetName)
    }
})

Fixes #205

@parruc
Copy link

parruc commented Jul 4, 2023

Would be nice to merge this pull request as the field does not work anymore in django >= 4.1

@guilleijo
Copy link
Author

Would be nice to merge this pull request as the field does not work anymore in django >= 4.1

Yeah but it looks like this is not being maintained anymore.

What you can do to fix this locally in your project is the following:

  1. Create a custom js file with the updated code from django_better_admin_arrayfield.js. For example: <app_name>/static/js/custom_django_better_admin_arrayfield.js
  2. Use the custom js in the admin class that you need.
class SomeModelInline(admin.StackedInline):

    class Media:
        js = ("js/custom_django_better_admin_arrayfield.js",)

@parruc
Copy link

parruc commented Jul 5, 2023

Yeah I guess it's not maintained anymore.

Because I don't like to put unmaintained packages in my projects I moved to https://github.com/bhch/django-jsonform that is awesome and looks very well maintained too.

Not exactly the same thing but the same effect can be achieved.

Thanks for the answer anyway. It could help somebody else.

@guilleijo
Copy link
Author

Cool I'll take a look at that package. Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inline forms not working after django 4.1
2 participants