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

CharWidget optimisations #1414

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Breaking changes

This release makes some minor changes to the public API. If you have overridden any methods from the `resources` module, you may need to update your implementation to accommodate these changes.

- Check value of ManyToManyField in skip_row() (#1271)
- Check value of `ManyToManyField` in skip_row() (#1271)
- This fixes an issue where ManyToMany fields are not checked correctly in `skip_row()`. This means that `skip_row()` now takes `row` as a mandatory arg. If you have overridden `skip_row()` in your own implementation, you will need to add `row` as an arg.

- Bug fix: validation errors were being ignored when `skip_unchanged` is set (#1378)
Expand All @@ -36,7 +36,8 @@ Enhancements

- Default format selections set correctly for export action (#1389)
- Added option to store raw row values in each row's `RowResult` (#1393)
- Add natural key support to ForeignKeyWidget (#1371)
- Add natural key support to `ForeignKeyWidget` (#1371)
- Optimised default instantiation of `CharWidget` (#1414)

Development
###########
Expand Down
1 change: 1 addition & 0 deletions import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@ class ModelResource(Resource, metaclass=ModelDeclarativeMetaclass):
'ManyToManyField': 'get_m2m_widget',
'OneToOneField': 'get_fk_widget',
'ForeignKey': 'get_fk_widget',
'CharField': widgets.CharWidget,
'DecimalField': widgets.DecimalWidget,
'DateTimeField': widgets.DateTimeWidget,
'DateField': widgets.DateWidget,
Expand Down
4 changes: 1 addition & 3 deletions import_export/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ class CharWidget(Widget):
"""
Widget for converting text fields.
"""

def render(self, value, obj=None):
return force_str(value)
pass


class BooleanWidget(Widget):
Expand Down
10 changes: 10 additions & 0 deletions tests/core/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,15 @@ def test_create(self):
self.assertEqual(BookResource._meta.model, Book)


class WidgetFromDjangoFieldTest(TestCase):

def test_widget_from_django_field_for_CharField_returns_CharWidget(self):
f = CharField()
resource = BookResource()
w = resource.widget_from_django_field(f)
self.assertEqual(widgets.CharWidget, w)


@skipUnless(
'postgresql' in settings.DATABASES['default']['ENGINE'],
'Run only against Postgres')
Expand All @@ -1327,6 +1336,7 @@ def test_widget_from_django_field_for_ArrayField_returns_SimpleArrayWidget(self)
res = resource.widget_from_django_field(f)
self.assertEqual(widgets.SimpleArrayWidget, res)


pokken-magic marked this conversation as resolved.
Show resolved Hide resolved
if 'postgresql' in settings.DATABASES['default']['ENGINE']:
from django.contrib.postgres.fields import ArrayField
from django.db import models
Expand Down
22 changes: 22 additions & 0 deletions tests/core/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@
from import_export import widgets


class WidgetTest(TestCase):
def setUp(self):
self.widget = widgets.Widget()

def test_clean(self):
self.assertEqual("a", self.widget.clean("a"))

def test_render(self):
self.assertEqual("1", self.widget.render(1))


class CharWidgetTest(TestCase):
def setUp(self):
self.widget = widgets.CharWidget()

def test_clean(self):
self.assertEqual("a", self.widget.clean("a"))

def test_render(self):
self.assertEqual("1", self.widget.render(1))


class BooleanWidgetTest(TestCase):

def setUp(self):
Expand Down