Repository: django-import-export/django-import-export (language: python)

Issue:
The get_export_resource_kwargs() doesn't contain `export_form` in v4, example from docs doesn't work
**Describe the bug**
I am using approach similar to [this example in the docs](get_export_resource_kwargs) to add custom field in my export form in ModelAdmin.
With version 4 I have got following error:

```exception
======================================================================
ERROR: test_export_with_date_filter (blenderhub.apps.admin_overrides.tests.test_admin_payments.TestAccountExport.test_export_with_date_filter)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/petr/soubory/programovani/blenderkit/blenderhub_server/blenderhub/apps/admin_overrides/tests/test_admin_payments.py", line 789, in test_export_with_date_filter
    response = self.client.post(
               ^^^^^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/test/client.py", line 948, in post
    response = super().post(
               ^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/test/client.py", line 482, in post
    return self.generic(
           ^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/test/client.py", line 609, in generic
    return self.request(**r)
           ^^^^^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/test/client.py", line 891, in request
    self.check_exception(response)
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/test/client.py", line 738, in check_exception
    raise exc_value
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/sentry_sdk/integrations/django/views.py", line 84, in sentry_wrapped_callback
    return callback(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/utils/decorators.py", line 134, in _wrapper_view
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/views/decorators/cache.py", line 62, in _wrapper_view_func
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/import_export/admin.py", line 764, in export_action
    context = self.init_request_context_data(request, form)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/import_export/admin.py", line 795, in init_request_context_data
    context["fields_list"] = [
                             ^
  File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.11/lib/python3.11/site-packages/import_export/admin.py", line 801, in <listcomp>
    **self.get_export_resource_kwargs(request)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/petr/soubory/programovani/blenderkit/blenderhub_server/blenderhub/apps/admin_overrides/admin.py", line 234, in get_export_resource_kwargs
    export_form = kwargs["export_form"]
                  ~~~~~~^^^^^^^^^^^^^^^
KeyError: 'export_form'
```

Since the code in `django-import-export` is this:

```python
    def init_request_context_data(self, request, form):
        context = self.get_export_context_data()
        context.update(self.admin_site.each_context(request))
        context["title"] = _("Export")
        context["form"] = form
        context["opts"] = self.model._meta
        context["fields_list"] = [
            (
                res.get_display_name(),
                [
                    field.column_name
                    for field in res(
                        **self.get_export_resource_kwargs(request)
                    ).get_user_visible_fields()
                ],
            )
            for res in self.get_export_resource_classes(request)
        ]
        return context
```

I expect that `export_form` is not passed anymore in `kwargs`.


My code is following:

```python
class CustomExportForm(ExportForm):
    date_to = forms.DateField(
        label="Date to",
        required=False,
        widget=forms.DateInput(attrs={"type": "date"}),
        help_text="If filled in, filter state of the accounts to this date<br>"
        "calculation will be much slower",
    )

@admin.register(Account)
class BKAccountAdmin(ImportExportMixin, RelatedFieldAdmin, admin.ModelAdmin):
    resource_classes = [AccountExportResource]
    export_form_class = CustomExportForm

    def get_export_resource_kwargs(self, request, *args, **kwargs):
        export_form = kwargs["export_form"]
        self.date_to = export_form.cleaned_data["date_to"]
        return {"date_to": self.date_to}
```


**Versions (please complete the following information):**
 - Django Import Export: 4.0.6
 - Python 3.11
 - Django 4.2.11

**Expected behavior**
Work accordingly to the example in the docs.



Discussion / hints:

Fix patch:
diff --git a/docs/admin_integration.rst b/docs/admin_integration.rst
index 351ae8f31..f9528106a 100644
--- a/docs/admin_integration.rst
+++ b/docs/admin_integration.rst
@@ -352,11 +352,11 @@ Customize ``ModelAdmin`` (for example see ``tests/core/admin.py``)::
         resource_classes = [EBookResource]
         export_form_class = CustomExportForm
 
-        def get_export_resource_kwargs(self, request, *args, **kwargs):
-            export_form = kwargs["export_form"]
+        def get_export_resource_kwargs(self, request, **kwargs):
+            export_form = kwargs.get("export_form")
             if export_form:
-                return dict(author_id=export_form.cleaned_data["author"].id)
-            return {}
+                kwargs.update(author_id=export_form.cleaned_data["author"].id)
+            return kwargs
 
     admin.site.register(Book, CustomBookAdmin)
 
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 6ef92953e..69ccf5c84 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -5,9 +5,10 @@ Changelog
 
     Version 4 introduces breaking changes.  Please refer to :doc:`release notes<release_notes>`.
 
-4.0.7 (unreleased)
+4.0.7 (2024-05-30)
 ------------------
 
+- fix documentation to show correct method for reading form data on export (`1859 <https://github.com/django-import-export/django-import-export/pull/1859>`_)
 - Admin UI: display both field name and column_name on export (`1857 <https://github.com/django-import-export/django-import-export/pull/1857>`_)
 
 4.0.6 (2024-05-27)


Test patch:
diff --git a/tests/core/forms.py b/tests/core/forms.py
index 05aaf8362..7d41398b4 100644
--- a/tests/core/forms.py
+++ b/tests/core/forms.py
@@ -26,6 +26,6 @@ class CustomConfirmImportForm(AuthorFormMixin, ConfirmImportForm):
 
 
 class CustomExportForm(AuthorFormMixin, SelectableFieldsExportForm):
-    """Customized ExportForm, with author field required"""
+    """Customized ExportForm, with author field required."""
 
-    pass
+    author = forms.ModelChoiceField(queryset=Author.objects.all(), required=True)
diff --git a/tests/core/tests/admin_integration/test_export.py b/tests/core/tests/admin_integration/test_export.py
index c7f3f94ec..3ed7a0e4b 100644
--- a/tests/core/tests/admin_integration/test_export.py
+++ b/tests/core/tests/admin_integration/test_export.py
@@ -525,3 +525,35 @@ def test_export_with_custom_field(self):
             f"{book.id},,Moonraker,1955-04-05\r\n"
         )
         self.assertEqual(str.encode(s), response.content)
+
+
+class FilteredExportTest(AdminTestMixin, TestCase):
+    """
+    Tests that exports can be filtered by a custom form field.
+    This process is demonstrated in the documentation.
+    """
+
+    def test_filtered_export(self):
+        a1 = Author.objects.create(id=11, name="Ian Fleming")
+        a2 = Author.objects.create(id=12, name="James Joyce")
+        b1 = Book.objects.create(name="Moonraker", author=a1)
+        b2 = Book.objects.create(name="Ulysses", author=a2)
+        response = self.client.get(self.ebook_export_url)
+        self.assertEqual(response.status_code, 200)
+        data = {
+            "format": "0",
+            "author": a1.id,
+            "resource": "",
+            "ebookresource_id": True,
+            "ebookresource_name": True,
+        }
+        response = self.client.post(self.ebook_export_url, data)
+        self.assertEqual(response.status_code, 200)
+        s = "id,name\r\n" f"{b1.id},Moonraker\r\n"
+        self.assertEqual(str.encode(s), response.content)
+
+        data["author"] = a2.id
+        response = self.client.post(self.ebook_export_url, data)
+        self.assertEqual(response.status_code, 200)
+        s = "id,name\r\n" f"{b2.id},Ulysses\r\n"
+        self.assertEqual(str.encode(s), response.content)