Added warning and test case for it
This commit is contained in:
parent
9535b03fd0
commit
c2f31af74b
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
import warnings
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib.postgres import lookups
|
from django.contrib.postgres import lookups
|
||||||
|
@ -8,6 +9,7 @@ from django.core import checks, exceptions
|
||||||
from django.db.models import Field, Func, IntegerField, Transform, Value
|
from django.db.models import Field, Func, IntegerField, Transform, Value
|
||||||
from django.db.models.fields.mixins import CheckFieldDefaultMixin
|
from django.db.models.fields.mixins import CheckFieldDefaultMixin
|
||||||
from django.db.models.lookups import Exact, In
|
from django.db.models.lookups import Exact, In
|
||||||
|
from django.utils.deprecation import RemovedInDjango51Warning
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from ..utils import prefix_validation_error
|
from ..utils import prefix_validation_error
|
||||||
|
@ -204,22 +206,22 @@ class ArrayField(CheckFieldDefaultMixin, Field):
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
if self.base_field.choices and "choices_form_class" not in kwargs:
|
if self.base_field.choices and "choices_form_class" not in kwargs:
|
||||||
|
obj = self.base_field
|
||||||
defaults = {
|
defaults = {
|
||||||
"choices_form_class": forms.TypedMultipleChoiceField,
|
"choices_form_class": forms.TypedMultipleChoiceField,
|
||||||
"coerce": self.base_field.to_python,
|
|
||||||
}
|
}
|
||||||
defaults.update(kwargs)
|
else:
|
||||||
return self.base_field.formfield(**defaults)
|
obj = super()
|
||||||
elif not self.choices:
|
|
||||||
defaults = {
|
defaults = {
|
||||||
"form_class": SimpleArrayField,
|
"form_class": SimpleArrayField,
|
||||||
"base_field": self.base_field.formfield(),
|
"base_field": self.base_field.formfield(),
|
||||||
"max_length": self.size,
|
"max_length": self.size,
|
||||||
}
|
}
|
||||||
defaults.update(kwargs)
|
if self.choices:
|
||||||
else:
|
warnings.warn(
|
||||||
raise NotImplementedError("Choices should be defined in base field.")
|
"Choices should be defined in base field.", RemovedInDjango51Warning
|
||||||
return super().formfield(**defaults)
|
)
|
||||||
|
return obj.formfield(**{**defaults, **kwargs})
|
||||||
|
|
||||||
|
|
||||||
class ArrayRHSMixin:
|
class ArrayRHSMixin:
|
||||||
|
|
|
@ -1024,7 +1024,7 @@ class Field(RegisterLookupMixin):
|
||||||
self.has_default() or "initial" in kwargs
|
self.has_default() or "initial" in kwargs
|
||||||
)
|
)
|
||||||
defaults["choices"] = self.get_choices(include_blank=include_blank)
|
defaults["choices"] = self.get_choices(include_blank=include_blank)
|
||||||
defaults["coerce"] = kwargs.get("coerce", self.to_python)
|
defaults["coerce"] = self.to_python
|
||||||
if self.null:
|
if self.null:
|
||||||
defaults["empty_value"] = None
|
defaults["empty_value"] = None
|
||||||
if choices_form_class is not None:
|
if choices_form_class is not None:
|
||||||
|
|
|
@ -3,6 +3,7 @@ import enum
|
||||||
import json
|
import json
|
||||||
import unittest
|
import unittest
|
||||||
import uuid
|
import uuid
|
||||||
|
import warnings
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.core import checks, exceptions, serializers, validators
|
from django.core import checks, exceptions, serializers, validators
|
||||||
|
@ -14,6 +15,7 @@ from django.db.models.functions import Cast, JSONObject, Upper
|
||||||
from django.test import TransactionTestCase, modify_settings, override_settings
|
from django.test import TransactionTestCase, modify_settings, override_settings
|
||||||
from django.test.utils import isolate_apps
|
from django.test.utils import isolate_apps
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.utils.deprecation import RemovedInDjango51Warning
|
||||||
|
|
||||||
from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase, PostgreSQLWidgetTestCase
|
from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase, PostgreSQLWidgetTestCase
|
||||||
from .models import (
|
from .models import (
|
||||||
|
@ -1134,12 +1136,15 @@ class TestChoiceFormField(PostgreSQLTestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_model_field_formfield_choices(self):
|
def test_model_field_formfield_choices(self):
|
||||||
with self.assertRaises(NotImplementedError):
|
|
||||||
model_field = ArrayField(
|
model_field = ArrayField(
|
||||||
models.CharField(max_length=27), choices=(("a1", "A1"), ("b1", "B1"))
|
models.CharField(max_length=27), choices=(("a1", "A1"), ("b1", "B1"))
|
||||||
)
|
)
|
||||||
form_field = model_field.formfield()
|
with warnings.catch_warnings(record=True) as w:
|
||||||
form_field.clean(["a1", "b1"])
|
warnings.simplefilter("always")
|
||||||
|
model_field.formfield()
|
||||||
|
assert len(w) == 1
|
||||||
|
assert issubclass(w[-1].category, RemovedInDjango51Warning)
|
||||||
|
assert "Choices should be defined in base field." in str(w[-1].message)
|
||||||
|
|
||||||
|
|
||||||
class TestSplitFormField(PostgreSQLSimpleTestCase):
|
class TestSplitFormField(PostgreSQLSimpleTestCase):
|
||||||
|
|
Loading…
Reference in New Issue