2017-05-12 09:04:52 +08:00
|
|
|
import pickle
|
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
from django import forms
|
2019-01-05 04:03:53 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-03-22 09:06:54 +08:00
|
|
|
from django.db import models
|
|
|
|
from django.test import SimpleTestCase, TestCase
|
2018-04-20 22:37:29 +08:00
|
|
|
from django.utils.functional import lazy
|
2009-04-12 13:32:23 +08:00
|
|
|
|
2013-05-03 22:02:10 +08:00
|
|
|
from .models import (
|
2019-01-05 04:03:53 +08:00
|
|
|
Bar, Choiceful, Foo, RenamedField, VerboseNameField, Whiz, WhizDelayed,
|
|
|
|
WhizIter, WhizIterEmpty,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2009-04-12 13:32:23 +08:00
|
|
|
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2017-06-26 22:31:32 +08:00
|
|
|
class Nested:
|
2017-06-14 20:41:02 +08:00
|
|
|
class Field(models.Field):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class BasicFieldTests(SimpleTestCase):
|
2016-03-22 09:06:54 +08:00
|
|
|
|
2010-03-07 09:50:58 +08:00
|
|
|
def test_show_hidden_initial(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
Fields with choices respect show_hidden_initial as a kwarg to
|
|
|
|
formfield().
|
2010-03-07 09:50:58 +08:00
|
|
|
"""
|
|
|
|
choices = [(0, 0), (1, 1)]
|
|
|
|
model_field = models.Field(choices=choices)
|
|
|
|
form_field = model_field.formfield(show_hidden_initial=True)
|
|
|
|
self.assertTrue(form_field.show_hidden_initial)
|
|
|
|
|
|
|
|
form_field = model_field.formfield(show_hidden_initial=False)
|
|
|
|
self.assertFalse(form_field.show_hidden_initial)
|
|
|
|
|
2011-05-03 19:51:37 +08:00
|
|
|
def test_field_repr(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
__repr__() of a field displays its name.
|
2011-05-03 19:51:37 +08:00
|
|
|
"""
|
|
|
|
f = Foo._meta.get_field('a')
|
|
|
|
self.assertEqual(repr(f), '<django.db.models.fields.CharField: a>')
|
|
|
|
f = models.fields.CharField()
|
|
|
|
self.assertEqual(repr(f), '<django.db.models.fields.CharField>')
|
|
|
|
|
2017-06-14 20:41:02 +08:00
|
|
|
def test_field_repr_nested(self):
|
|
|
|
"""__repr__() uses __qualname__ for nested class support."""
|
|
|
|
self.assertEqual(repr(Nested.Field()), '<model_fields.tests.Nested.Field>')
|
|
|
|
|
2011-08-13 19:53:42 +08:00
|
|
|
def test_field_name(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
A defined field name (name="fieldname") is used instead of the model
|
|
|
|
model's attribute name (modelname).
|
2011-08-13 19:53:42 +08:00
|
|
|
"""
|
|
|
|
instance = RenamedField()
|
|
|
|
self.assertTrue(hasattr(instance, 'get_fieldname_display'))
|
|
|
|
self.assertFalse(hasattr(instance, 'get_modelname_display'))
|
|
|
|
|
2012-09-02 00:32:27 +08:00
|
|
|
def test_field_verbose_name(self):
|
|
|
|
m = VerboseNameField
|
2016-12-31 23:30:41 +08:00
|
|
|
for i in range(1, 23):
|
2016-03-22 09:06:54 +08:00
|
|
|
self.assertEqual(m._meta.get_field('field%d' % i).verbose_name, 'verbose field%d' % i)
|
2012-09-02 00:32:27 +08:00
|
|
|
|
|
|
|
self.assertEqual(m._meta.get_field('id').verbose_name, 'verbose pk')
|
|
|
|
|
2013-08-30 11:44:37 +08:00
|
|
|
def test_choices_form_class(self):
|
2016-03-22 09:06:54 +08:00
|
|
|
"""Can supply a custom choices form class to Field.formfield()"""
|
2013-08-30 11:44:37 +08:00
|
|
|
choices = [('a', 'a')]
|
2013-02-24 02:01:38 +08:00
|
|
|
field = models.CharField(choices=choices)
|
2013-08-30 11:44:37 +08:00
|
|
|
klass = forms.TypedMultipleChoiceField
|
|
|
|
self.assertIsInstance(field.formfield(choices_form_class=klass), klass)
|
2013-02-24 02:01:38 +08:00
|
|
|
|
2017-03-22 07:29:14 +08:00
|
|
|
def test_formfield_disabled(self):
|
|
|
|
"""Field.formfield() sets disabled for fields with choices."""
|
|
|
|
field = models.CharField(choices=[('a', 'b')])
|
|
|
|
form_field = field.formfield(disabled=True)
|
|
|
|
self.assertIs(form_field.disabled, True)
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_field_str(self):
|
2016-12-15 03:04:26 +08:00
|
|
|
f = models.Field()
|
|
|
|
self.assertEqual(str(f), '<django.db.models.fields.Field>')
|
2014-01-20 10:45:21 +08:00
|
|
|
f = Foo._meta.get_field('a')
|
2016-12-15 03:04:26 +08:00
|
|
|
self.assertEqual(str(f), 'model_fields.Foo.a')
|
2015-02-12 14:28:24 +08:00
|
|
|
|
2016-12-15 00:48:57 +08:00
|
|
|
def test_field_ordering(self):
|
|
|
|
"""Fields are ordered based on their creation."""
|
|
|
|
f1 = models.Field()
|
|
|
|
f2 = models.Field(auto_created=True)
|
|
|
|
f3 = models.Field()
|
|
|
|
self.assertLess(f2, f1)
|
|
|
|
self.assertGreater(f3, f1)
|
|
|
|
self.assertIsNotNone(f1)
|
|
|
|
self.assertNotIn(f2, (None, 1, ''))
|
|
|
|
|
2017-05-12 09:04:52 +08:00
|
|
|
def test_field_instance_is_picklable(self):
|
|
|
|
"""Field instances can be pickled."""
|
|
|
|
field = models.Field(max_length=100, default='a string')
|
|
|
|
# Must be picklable with this cached property populated (#28188).
|
|
|
|
field._get_default
|
|
|
|
pickle.dumps(field)
|
|
|
|
|
2017-06-14 20:41:02 +08:00
|
|
|
def test_deconstruct_nested_field(self):
|
|
|
|
"""deconstruct() uses __qualname__ for nested class support."""
|
|
|
|
name, path, args, kwargs = Nested.Field().deconstruct()
|
|
|
|
self.assertEqual(path, 'model_fields.tests.Nested.Field')
|
|
|
|
|
2016-01-10 07:05:57 +08:00
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
class ChoicesTests(SimpleTestCase):
|
2016-01-10 07:05:57 +08:00
|
|
|
|
2019-01-05 04:03:53 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super().setUpClass()
|
|
|
|
cls.no_choices = Choiceful._meta.get_field('no_choices')
|
|
|
|
cls.empty_choices = Choiceful._meta.get_field('empty_choices')
|
|
|
|
cls.empty_choices_bool = Choiceful._meta.get_field('empty_choices_bool')
|
|
|
|
cls.empty_choices_text = Choiceful._meta.get_field('empty_choices_text')
|
|
|
|
cls.with_choices = Choiceful._meta.get_field('with_choices')
|
|
|
|
|
|
|
|
def test_choices(self):
|
|
|
|
self.assertIsNone(self.no_choices.choices)
|
|
|
|
self.assertEqual(self.empty_choices.choices, ())
|
|
|
|
self.assertEqual(self.with_choices.choices, [(1, 'A')])
|
|
|
|
|
|
|
|
def test_flatchoices(self):
|
|
|
|
self.assertEqual(self.no_choices.flatchoices, [])
|
|
|
|
self.assertEqual(self.empty_choices.flatchoices, [])
|
|
|
|
self.assertEqual(self.with_choices.flatchoices, [(1, 'A')])
|
|
|
|
|
|
|
|
def test_check(self):
|
|
|
|
self.assertEqual(Choiceful.check(), [])
|
|
|
|
|
|
|
|
def test_invalid_choice(self):
|
|
|
|
model_instance = None # Actual model instance not needed.
|
|
|
|
self.no_choices.validate(0, model_instance)
|
|
|
|
msg = "['Value 99 is not a valid choice.']"
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
self.empty_choices.validate(99, model_instance)
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
self.with_choices.validate(99, model_instance)
|
|
|
|
|
|
|
|
def test_formfield(self):
|
|
|
|
no_choices_formfield = self.no_choices.formfield()
|
|
|
|
self.assertIsInstance(no_choices_formfield, forms.IntegerField)
|
|
|
|
fields = (
|
|
|
|
self.empty_choices, self.with_choices, self.empty_choices_bool,
|
|
|
|
self.empty_choices_text,
|
|
|
|
)
|
|
|
|
for field in fields:
|
|
|
|
with self.subTest(field=field):
|
|
|
|
self.assertIsInstance(field.formfield(), forms.ChoiceField)
|
|
|
|
|
|
|
|
|
|
|
|
class GetFieldDisplayTests(SimpleTestCase):
|
|
|
|
|
2009-04-11 03:54:14 +08:00
|
|
|
def test_choices_and_field_display(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
get_choices() interacts with get_FIELD_display() to return the expected
|
|
|
|
values.
|
2009-04-11 03:54:14 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(Whiz(c=1).get_c_display(), 'First') # A nested value
|
|
|
|
self.assertEqual(Whiz(c=0).get_c_display(), 'Other') # A top level value
|
|
|
|
self.assertEqual(Whiz(c=9).get_c_display(), 9) # Invalid value
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(Whiz(c=None).get_c_display()) # Blank value
|
2009-04-11 03:54:14 +08:00
|
|
|
self.assertEqual(Whiz(c='').get_c_display(), '') # Empty value
|
2019-01-05 04:03:53 +08:00
|
|
|
self.assertEqual(WhizDelayed(c=0).get_c_display(), 'Other') # Delayed choices
|
2009-04-12 13:32:23 +08:00
|
|
|
|
2019-03-24 00:04:39 +08:00
|
|
|
def test_get_FIELD_display_translated(self):
|
|
|
|
"""A translated display value is coerced to str."""
|
|
|
|
val = Whiz(c=5).get_c_display()
|
|
|
|
self.assertIsInstance(val, str)
|
|
|
|
self.assertEqual(val, 'translated')
|
|
|
|
|
2019-10-31 18:34:56 +08:00
|
|
|
def test_overriding_FIELD_display(self):
|
|
|
|
class FooBar(models.Model):
|
|
|
|
foo_bar = models.IntegerField(choices=[(1, 'foo'), (2, 'bar')])
|
|
|
|
|
|
|
|
def get_foo_bar_display(self):
|
|
|
|
return 'something'
|
|
|
|
|
|
|
|
f = FooBar(foo_bar=1)
|
|
|
|
self.assertEqual(f.get_foo_bar_display(), 'something')
|
|
|
|
|
2014-07-28 05:39:40 +08:00
|
|
|
def test_iterator_choices(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
get_choices() works with Iterators.
|
2014-07-28 05:39:40 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(WhizIter(c=1).c, 1) # A nested value
|
|
|
|
self.assertEqual(WhizIter(c=9).c, 9) # Invalid value
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(WhizIter(c=None).c) # Blank value
|
2014-07-28 05:39:40 +08:00
|
|
|
self.assertEqual(WhizIter(c='').c, '') # Empty value
|
|
|
|
|
|
|
|
def test_empty_iterator_choices(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
get_choices() works with empty iterators.
|
2014-07-28 05:39:40 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(WhizIterEmpty(c="a").c, "a") # A nested value
|
|
|
|
self.assertEqual(WhizIterEmpty(c="b").c, "b") # Invalid value
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(WhizIterEmpty(c=None).c) # Blank value
|
2014-07-28 05:39:40 +08:00
|
|
|
self.assertEqual(WhizIterEmpty(c='').c, '') # Empty value
|
2018-04-20 22:37:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GetChoicesTests(SimpleTestCase):
|
|
|
|
|
2019-01-05 04:03:53 +08:00
|
|
|
def test_empty_choices(self):
|
|
|
|
choices = []
|
|
|
|
f = models.CharField(choices=choices)
|
|
|
|
self.assertEqual(f.get_choices(include_blank=False), choices)
|
|
|
|
|
2018-04-20 22:37:29 +08:00
|
|
|
def test_blank_in_choices(self):
|
|
|
|
choices = [('', '<><>'), ('a', 'A')]
|
|
|
|
f = models.CharField(choices=choices)
|
|
|
|
self.assertEqual(f.get_choices(include_blank=True), choices)
|
|
|
|
|
2018-03-22 21:05:31 +08:00
|
|
|
def test_blank_in_grouped_choices(self):
|
|
|
|
choices = [
|
|
|
|
('f', 'Foo'),
|
|
|
|
('b', 'Bar'),
|
|
|
|
('Group', (
|
|
|
|
('', 'No Preference'),
|
|
|
|
('fg', 'Foo'),
|
|
|
|
('bg', 'Bar'),
|
|
|
|
)),
|
|
|
|
]
|
|
|
|
f = models.CharField(choices=choices)
|
|
|
|
self.assertEqual(f.get_choices(include_blank=True), choices)
|
|
|
|
|
2018-04-20 22:37:29 +08:00
|
|
|
def test_lazy_strings_not_evaluated(self):
|
|
|
|
lazy_func = lazy(lambda x: 0 / 0, int) # raises ZeroDivisionError if evaluated.
|
|
|
|
f = models.CharField(choices=[(lazy_func('group'), (('a', 'A'), ('b', 'B')))])
|
|
|
|
self.assertEqual(f.get_choices(include_blank=True)[0], ('', '---------'))
|
2018-10-19 21:41:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GetChoicesOrderingTests(TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2019-08-15 12:54:41 +08:00
|
|
|
cls.foo1 = Foo.objects.create(a='a', d='12.35')
|
2018-10-19 21:41:29 +08:00
|
|
|
cls.foo2 = Foo.objects.create(a='b', d='12.34')
|
2019-08-15 12:54:41 +08:00
|
|
|
cls.bar1 = Bar.objects.create(a=cls.foo1, b='b')
|
2018-10-19 21:41:29 +08:00
|
|
|
cls.bar2 = Bar.objects.create(a=cls.foo2, b='a')
|
|
|
|
cls.field = Bar._meta.get_field('a')
|
|
|
|
|
|
|
|
def assertChoicesEqual(self, choices, objs):
|
|
|
|
self.assertEqual(choices, [(obj.pk, str(obj)) for obj in objs])
|
|
|
|
|
|
|
|
def test_get_choices(self):
|
|
|
|
self.assertChoicesEqual(
|
|
|
|
self.field.get_choices(include_blank=False, ordering=('a',)),
|
|
|
|
[self.foo1, self.foo2]
|
|
|
|
)
|
|
|
|
self.assertChoicesEqual(
|
|
|
|
self.field.get_choices(include_blank=False, ordering=('-a',)),
|
|
|
|
[self.foo2, self.foo1]
|
|
|
|
)
|
|
|
|
|
2019-08-15 12:54:41 +08:00
|
|
|
def test_get_choices_default_ordering(self):
|
|
|
|
self.addCleanup(setattr, Foo._meta, 'ordering', Foo._meta.ordering)
|
|
|
|
Foo._meta.ordering = ('d',)
|
|
|
|
self.assertChoicesEqual(
|
|
|
|
self.field.get_choices(include_blank=False),
|
|
|
|
[self.foo2, self.foo1]
|
|
|
|
)
|
|
|
|
|
2018-10-19 21:41:29 +08:00
|
|
|
def test_get_choices_reverse_related_field(self):
|
|
|
|
self.assertChoicesEqual(
|
|
|
|
self.field.remote_field.get_choices(include_blank=False, ordering=('a',)),
|
|
|
|
[self.bar1, self.bar2]
|
|
|
|
)
|
|
|
|
self.assertChoicesEqual(
|
|
|
|
self.field.remote_field.get_choices(include_blank=False, ordering=('-a',)),
|
|
|
|
[self.bar2, self.bar1]
|
|
|
|
)
|
2019-08-15 12:54:41 +08:00
|
|
|
|
|
|
|
def test_get_choices_reverse_related_field_default_ordering(self):
|
|
|
|
self.addCleanup(setattr, Bar._meta, 'ordering', Bar._meta.ordering)
|
|
|
|
Bar._meta.ordering = ('b',)
|
|
|
|
self.assertChoicesEqual(
|
|
|
|
self.field.remote_field.get_choices(include_blank=False),
|
|
|
|
[self.bar2, self.bar1]
|
|
|
|
)
|