Skip to content

Commit

Permalink
fix issue with widget assignment for custom ForeignKey subclasses (#1826
Browse files Browse the repository at this point in the history
)

* fix issue with widget assignment for custom ForeignKey subclasses

* updated changelog
  • Loading branch information
matthewhegarty committed May 13, 2024
1 parent c69e32d commit 2f108b6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ Changelog

Version 4 introduces breaking changes. Please refer to :doc:`release notes<release_notes>`.

4.0.2 (unreleased)
4.0.2 (2024-05-13)
------------------

- fix export with custom column name (`1821 <https://github.com/django-import-export/django-import-export/pull/1821>`_)
- fix allow ``column_name`` to be declared in ``fields`` list (`1822 <https://github.com/django-import-export/django-import-export/pull/1822>`_)
- fix clash between ``key_is_id`` and ``use_natural_foreign_keys`` (`1824 <https://github.com/django-import-export/django-import-export/pull/1824>`_)
- remove unreachable code (`1825 <https://github.com/django-import-export/django-import-export/pull/1825>`_)
- fix issue with widget assignment for custom ``ForeignKey`` subclasses (`1826 <https://github.com/django-import-export/django-import-export/pull/1826>`_)

4.0.1 (2024-05-08)
------------------
Expand Down
2 changes: 2 additions & 0 deletions import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,8 @@ def widget_from_django_field(cls, f, default=widgets.Widget):
for base_class in inspect.getmro(f.__class__):
if base_class.__name__ in cls.WIDGETS_MAP:
result = cls.WIDGETS_MAP[base_class.__name__]
if isinstance(result, str):
result = getattr(cls, result)(f)
break

try:
Expand Down
22 changes: 21 additions & 1 deletion tests/core/tests/test_model_resource_fields_generate_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from django.contrib.postgres import search as postgres_search
from django.contrib.postgres.fields import ranges as postgres_ranges
from django.db import models
from django.db.models.fields.related import RelatedField
from django.db.models.fields.related import ForeignKey, RelatedField

from import_export import widgets
from import_export.resources import ModelResource
from import_export.widgets import ForeignKeyWidget


class ExampleResource(ModelResource):
Expand Down Expand Up @@ -188,3 +189,22 @@ def _get_django_fields_for_check_widget(self):
postgres.RangeField(),
]
return fields

def test_custom_fk_field(self):
# issue 1817 - if a 'custom' foreign key field is provided, then this should
# be handled when widgets are defined
class CustomForeignKey(ForeignKey):
def __init__(
self,
to,
on_delete,
**kwargs,
):
super().__init__(to, on_delete, **kwargs)

resource_field = ModelResource.field_from_django_field(
"custom_fk",
CustomForeignKey(WithPositiveIntegerFields, on_delete=models.SET_NULL),
False,
)
self.assertEqual(ForeignKeyWidget, resource_field.widget.__class__)

0 comments on commit 2f108b6

Please sign in to comment.