From 3e05a5c0ca2639dc3d22a0534ccff9e00be45374 Mon Sep 17 00:00:00 2001 From: jeffrey k eliasen Date: Tue, 12 Jun 2018 16:34:15 -1000 Subject: [PATCH] simple test demonstrating the issue --- rest_framework/fields.py | 2 +- tests/test_fields.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index d6e363339..f50276473 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1397,7 +1397,7 @@ class ChoiceField(Field): html_cutoff_text = _('More than {count} items...') def __init__(self, choices, **kwargs): - self.choices = choices + self._choices = choices self.html_cutoff = kwargs.pop('html_cutoff', self.html_cutoff) self.html_cutoff_text = kwargs.pop('html_cutoff_text', self.html_cutoff_text) diff --git a/tests/test_fields.py b/tests/test_fields.py index 7227c2f5a..2bbc9a24d 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1,6 +1,7 @@ import datetime import os import re +import sys import unittest import uuid from decimal import ROUND_DOWN, ROUND_UP, Decimal @@ -25,6 +26,13 @@ try: except ImportError: typings = False +if sys.version_info[0] < 3: + # python 2 + import mock +else: + # python 3 + from unittest import mock + # Tests for helper functions. # --------------------------- @@ -1524,6 +1532,17 @@ class TestChoiceField(FieldValues): ] ) + @mock.patch('rest_framework.fields.ChoiceField.choices') + def test_set_initial_choices(self, choices_mock): + field = serializers.ChoiceField( + allow_null=True, + choices=[ + 1, 2, + ] + ) + assert field._choices == [1, 2] + assert field.choices == choices_mock + def test_allow_blank(self): """ If `allow_blank=True` then '' is a valid input.