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

Fixed #30758 -- Made RangeFields use multiple hidden inputs for initial data. #11755

Closed
wants to merge 2 commits into from

Conversation

nasirhjafri
Copy link
Contributor

@nasirhjafri nasirhjafri changed the title Fixed #30758 DateTimeRangeField with default crashes in django admin Fixed #30758 Fixed #30758 Forced value to be string for BaseTemporalField Sep 8, 2019
@nasirhjafri nasirhjafri changed the title Fixed #30758 Fixed #30758 Forced value to be string for BaseTemporalField Fixed #30758 Forced value to be string for BaseTemporalField Sep 8, 2019
@nasirhjafri nasirhjafri force-pushed the ticket_30758 branch 2 times, most recently from a6e94a5 to 9565b91 Compare September 8, 2019 20:48
@felixxm
Copy link
Member

felixxm commented Sep 13, 2019

@nasirhjafri Thanks for this PR, however I don't think that it's a proper approach. IMO we should rather fix passing initial data for range fields. Currently we use HiddenInput which ends with

<input type="hidden"
       name="initial-range_field"
       value="[datetime.datetime(2019, 9, 13, 9, 53, 31, 531040), None]"
...

IMO we should change hidden_widget in BaseRangeField to get

<input type="hidden" name="initial-range_field_0" value="2019-09-13 09:53:31" id="initial-id_range_field_0">
<input type="hidden" name="initial-range_field_1" id="initial-id_range_field_1">

e.g.

diff --git a/django/contrib/postgres/forms/ranges.py b/django/contrib/postgres/forms/ranges.py
index 47a16a1bbd..2d55cbce57 100644
--- a/django/contrib/postgres/forms/ranges.py
+++ b/django/contrib/postgres/forms/ranges.py
@@ -2,7 +2,7 @@ from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
 
 from django import forms
 from django.core import exceptions
-from django.forms.widgets import MultiWidget
+from django.forms.widgets import MultiWidget, MultipleHiddenInput
 from django.utils.translation import gettext_lazy as _
 
 __all__ = [
@@ -16,6 +16,7 @@ class BaseRangeField(forms.MultiValueField):
         'invalid': _('Enter two valid values.'),
         'bound_ordering': _('The start of the range must not exceed the end of the range.'),
     }
+    hidden_widget = MultipleHiddenInput
 
     def __init__(self, **kwargs):
         if 'widget' not in kwargs:

@felixxm felixxm changed the title Fixed #30758 Forced value to be string for BaseTemporalField Fixed #30758 -- Made RangeFields use multiple hidden inputs for initial data. Sep 13, 2019
@nasirhjafri
Copy link
Contributor Author

@felixxm That's how it was being done in 1.11.x https://github.com/django/django/blob/stable/1.11.x/django/forms/fields.py#L398

I'll try the solution you provided, thanks.

@felixxm
Copy link
Member

felixxm commented Sep 13, 2019

@nasirhjafri Yes but it's clunky, IMO.

@nasirhjafri
Copy link
Contributor Author

@felixxm Any ideas on how test cases should look like? This solution works but I can't think of a test case.

@nasirhjafri nasirhjafri force-pushed the ticket_30758 branch 2 times, most recently from 9d34863 to 2a7b054 Compare September 13, 2019 11:46
@felixxm
Copy link
Member

felixxm commented Sep 13, 2019

We should test the way how RangeFields widgets are rendered with initial data (tests.postgres_tests.TestWidget) and how has_changed() works for these fields with and without initial data (see similar tests for MultiValueField).

@felixxm felixxm self-assigned this Sep 13, 2019
@nasirhjafri
Copy link
Contributor Author

nasirhjafri commented Sep 14, 2019

@felixxm I've pushed the tests, but they are not running on circle ci. Can you please retrigger the tests on PR?

Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nasirhjafri Please move tests to the postgres_tests.test_ranges.TestWidget. Moreover tests that are not related with this patch should be in a separate commit, i.e. first commit with extra tests and second (Fixed #30758 -- Made RangeFields use multiple hidden inputs for initial data.) with fix and tests that falling without it.


def test_date_range_has_changed_empty_initial(self):
self.assertTrue(self.date_field.has_changed(['2010-01-01', ''],
['2010-01-01', '2020-12-12']))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use hanging indentation per our coding style (in all tests).

@nasirhjafri nasirhjafri force-pushed the ticket_30758 branch 10 times, most recently from 3b7febf to 920afe9 Compare September 16, 2019 18:10
@nasirhjafri
Copy link
Contributor Author

@felixxm This one is failing without the fix and working with the fix.

def test_range_fields_form_with_initial_data(self):
data = QueryDict(mutable=True)
data.update({
'date_field_0': '2010-01-01',
'date_field_1': '',
'datetime_field_0': '2010-01-01 00:00:00',
'datetime_field_1': '',
'initial-date_field': '[datetime.date(2019, 9, 14), None]',
'initial-datetime_field': '[datetime.datetime(2019, 9, 14, 15, 52, 33), None]'
})
form = self.RangeFieldsForm(data=data)
self.assertTrue(form.has_changed())

I've created two separate commits. Please review.

@felixxm
Copy link
Member

felixxm commented Sep 17, 2019

@nasirhjafri Thanks! I simplified tests, moved them between test classes and added some extra tests. I also notice that with MultipleHiddenInput it doesn't detect changes as expected, so I decided to add a new HiddenRangeWidget which works fine, IMO. Can you take a look?

@nasirhjafri
Copy link
Contributor Author

@felixxm looks good to me, working fine with the new HiddenRangeWidget.

@felixxm
Copy link
Member

felixxm commented Sep 17, 2019

Great, I squashed commits and rebased from master.

@felixxm
Copy link
Member

felixxm commented Sep 17, 2019

Merged in faf4b98 and 733dbb2.

@felixxm felixxm closed this Sep 17, 2019
@nasirhjafri nasirhjafri deleted the ticket_30758 branch September 17, 2019 10:20
@yeppus
Copy link

yeppus commented Sep 19, 2019

Hi! Thanks a lot for fixing this!

@felixxm do you know if this patch will be able to make it into 2.2.6?

@felixxm
Copy link
Member

felixxm commented Sep 19, 2019

@yeppus No, it doesn't qualify for a backport to 2.2.x, but it's included in Django 3.0.

@yeppus
Copy link

yeppus commented Sep 19, 2019

@felixxm thanks for letting me know,,, we need the LTS so I guess we need to rework the code around this bug in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants