Skip to content
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

Include custom form media in templates #1038

Merged

Conversation

andreynovikov
Copy link
Contributor

Problem

When custom import/export forms contain complex widgets with linked media it is not added to output.

Solution

Added standard form.media to templates.

Acceptance Criteria

This fix does not require tests and documentation changes.

@coveralls
Copy link

coveralls commented Nov 21, 2019

Coverage Status

Coverage decreased (-0.08%) to 95.519% when pulling fc7fe0a on andreynovikov:form-media-fix into 3cf5e3f on django-import-export:master.

@stale
Copy link

stale bot commented May 19, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Stale bot label May 19, 2020
@andrewgy8 andrewgy8 removed the stale Stale bot label May 31, 2020
@andrewgy8
Copy link
Member

Hey @andreynovikov . Thanks for the PR. When you say

custom import/export forms contain complex widgets

Can you give an example of this and some screeshots? Also how does this effect current/standard behavior?

@ababic
Copy link
Contributor

ababic commented Jun 6, 2020

I've also ran into this problem. @andrewgy8 you can recreate this in the test app by updating tests.core.forms.AuthorFormMixin like so:

from django.contrib import admin
from django.contrib.admin.widgets import AutocompleteSelect


class AuthorFormMixin(forms.Form):
    author = forms.ModelChoiceField(
        queryset=Author.objects.all(),
        widget=AutocompleteSelect(Author.objects.all(), admin_site=admin.site, using=Author.objects.all().db)
    )

If the form media is loaded correctly, the field should use the nice autocomplete widget you get in an edit form when you use autocomplete_fields on a ModelAdmin instance. When it isn't, you see a standard select field without any options (because the JS to load them isn't included).

@andrewgy8
Copy link
Member

Hey! Is this safe to be merged to master?

@matthewhegarty matthewhegarty self-assigned this Apr 14, 2022
@matthewhegarty
Copy link
Contributor

I would be OK with including this but I would like to understand exactly how it works. Can anyone supply steps to reproduce / screenshots / or a more detailed explanation of what's going on. I read Andy's comment but I'm still not clear on what the nature of this change is.

@ababic
Copy link
Contributor

ababic commented Apr 17, 2022

@matthewhegarty In a nutshell, Django has a mechanism that allows form widgets to register additional JS and CSS so that they can function properly. However, if you do not render form.media (a collation of all the JS and CSS required for all fields/widgets used for that form), that JS and CSS is unavailable in the front-end, and widgets do not look or function correctly.

@matthewhegarty
Copy link
Contributor

matthewhegarty commented Apr 17, 2022

@ababic Andy - thanks for the explanation, I understand what's going on now. I was able to reproduce the issue in the v3 codebase as follows (adapted from Andy's post):

forms.py

class AuthorFormMixin(forms.Form):
    author = forms.ModelChoiceField(
        queryset=Author.objects.all(),
        widget=AutocompleteSelect(Book._meta.get_field('author'), admin.site)
    )

#...

class CustomExportForm(AuthorFormMixin, ExportForm):
    """Customized ExportForm, with author field required"""
    pass

admin.py

class AuthorAdmin(ImportMixin, admin.ModelAdmin):
    search_fields = ['name']


class CustomBookAdmin(BookAdmin):
    """BookAdmin with custom import forms"""
    autocomplete_fields = ['author']

# ...

    def get_export_form(self):
        return CustomExportForm

(then add the PR changes to import.html & export.html)

This results in the correct autocomplete widget being show, with 'author' values being dynamically populated.

admin-site-author-select

admin-site-author-select-2

admin-site-export

@matthewhegarty matthewhegarty changed the base branch from main to release-3-x April 17, 2022 18:07
@matthewhegarty
Copy link
Contributor

I've updated this PR with merges from v3.
I have tested this change manually in the admin site.

@matthewhegarty
Copy link
Contributor

Thank you @andreynovikov and @ababic

@ababic
Copy link
Contributor

ababic commented Apr 17, 2022

Thanks @matthewhegarty! Happy Easter to you :)

@matthewhegarty matthewhegarty merged commit b16037d into django-import-export:release-3-x Apr 17, 2022
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.

None yet

5 participants