1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #24561 -- Added support for callables on model fields' choices.

This commit is contained in:
Natalia
2023-08-31 09:09:30 -03:00
parent 5bfb3cbf49
commit 691f70c477
9 changed files with 101 additions and 31 deletions

View File

@@ -4,6 +4,7 @@ from django import forms
from django.core.exceptions import ValidationError
from django.db import models
from django.test import SimpleTestCase, TestCase
from django.utils.choices import CallableChoiceIterator
from django.utils.functional import lazy
from .models import (
@@ -162,6 +163,7 @@ class ChoicesTests(SimpleTestCase):
)
cls.choices_from_enum = Choiceful._meta.get_field("choices_from_enum")
cls.choices_from_iterator = Choiceful._meta.get_field("choices_from_iterator")
cls.choices_from_callable = Choiceful._meta.get_field("choices_from_callable")
def test_choices(self):
self.assertIsNone(self.no_choices.choices)
@@ -174,6 +176,12 @@ class ChoicesTests(SimpleTestCase):
self.assertEqual(
self.choices_from_iterator.choices, [(0, "0"), (1, "1"), (2, "2")]
)
self.assertIsInstance(
self.choices_from_callable.choices, CallableChoiceIterator
)
self.assertEqual(
self.choices_from_callable.choices.func(), [(0, "0"), (1, "1"), (2, "2")]
)
def test_flatchoices(self):
self.assertEqual(self.no_choices.flatchoices, [])
@@ -186,6 +194,9 @@ class ChoicesTests(SimpleTestCase):
self.assertEqual(
self.choices_from_iterator.flatchoices, [(0, "0"), (1, "1"), (2, "2")]
)
self.assertEqual(
self.choices_from_callable.flatchoices, [(0, "0"), (1, "1"), (2, "2")]
)
def test_check(self):
self.assertEqual(Choiceful.check(), [])
@@ -204,9 +215,14 @@ class ChoicesTests(SimpleTestCase):
self.assertIsInstance(no_choices_formfield, forms.IntegerField)
fields = (
self.empty_choices,
self.with_choices,
self.empty_choices_bool,
self.empty_choices_text,
self.with_choices,
self.with_choices_dict,
self.with_choices_nested_dict,
self.choices_from_enum,
self.choices_from_iterator,
self.choices_from_callable,
)
for field in fields:
with self.subTest(field=field):