Skip to content

Commit

Permalink
Fix ModelForm issue in django 3.0+, which caused fields with required…
Browse files Browse the repository at this point in the history
…=False validation by model fields (#19)

1. Fix ModelForm issue in django 3.0+, which caused fields with required=False validation by model fields
2. Replaced django 2.1 with django 4.0 in test matrix
  • Loading branch information
M1ha-Shvn authored Jan 13, 2022
1 parent 7c11ee5 commit 6160188
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ jobs:
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
django-version: ["2.1", "2.2", "3.0", "3.1", "3.2"]
django-version: ["2.2", "3.0", "3.1", "3.2", "4.0"]
exclude:
# Django 4.0+ doesn't support python 3.6, 3.7
- python-version: "3.6"
django-version: "4.0"
- python-version: "3.7"
django-version: "4.0"

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ WORKDIR /app/src
# Install dependencies
# set -eu "breaks" pipeline on first error
COPY ./requirements-test.txt /app/requirements-test.txt
RUN --mount=type=cache,target=/root/.cache/pip \
set -eu && \
RUN set -eu && \
python3 -m pip install --upgrade pip setuptools wheel && \
python3 -m pip install --upgrade --requirement /app/requirements-test.txt

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Extended form fields to validate REST-request data via django.forms.
## Requirements
* Python 3.6+
Other versions may also work but not tested automatically
* Django 2.1+
* Django 2.2+
Other versions may also work but not tested automatically
* jsonschema
* pytz
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
description-file = README.md
description_file = README.md

[bdist_wheel]
universal = 1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name='django-rest-form-fields',
version='1.3.3',
version='1.3.4',
packages=['django_rest_form_fields'],
package_dir={'': 'src'},
url='https://github.com/M1hacka/django-rest-form-fields',
Expand Down
2 changes: 1 addition & 1 deletion src/django_rest_form_fields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class EmptyStringFixMixing(BaseField):
# 2) RestCharField and child classes don't allow to pass empty string if required=True
# 3) run_validators() method is not called if value is in empty_values, so min_length validation doesn't work
# for RestCharField and value=''
empty_values = []
empty_values = [None]

def to_python(self, value): # type: (Any) -> Optional[str]
return None if value is None else super(EmptyStringFixMixing, self).to_python(value)
Expand Down
37 changes: 37 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,40 @@ def test_initial(self):
{'dest': 'init', 'model_field': 'm'})
self._test_form(BaseModelForm, {'dest': RestCharField(source='src', required=False, initial='init')},
{'dest': 'test', 'model_field': 'm'})


class RequiredBaseModelFormTest(TestCase):
def test_base_form(self):
class TestForm(BaseForm):
test = RestCharField(max_length=255, required=False)

f = TestForm({})
f.full_clean()
self.assertTrue(f.is_valid())
self.assertDictEqual({'test': None}, f.cleaned_data)

def test_model_form(self):
class TestForm(BaseModelForm):
class Meta:
model = ModelExample
fields = ('model_field',)

model_field = RestCharField(max_length=255, required=False)

f = TestForm({})
f.full_clean()
self.assertTrue(f.is_valid())
self.assertDictEqual({'model_field': None}, f.cleaned_data)

def test_model_form_initial(self):
class TestForm(BaseModelForm):
class Meta:
model = ModelExample
fields = ('model_field',)

model_field = RestCharField(max_length=255, required=False, initial='init')

f = TestForm({})
f.full_clean()
self.assertTrue(f.is_valid())
self.assertDictEqual({'model_field': 'init'}, f.cleaned_data)

0 comments on commit 6160188

Please sign in to comment.