fix for #354 - allow None for nullable field with choices
This commit is contained in:
@ -52,8 +52,8 @@ def convert_choices_if_needed( # noqa: CCR001
|
|||||||
|
|
||||||
:param field: ormar field to check with choices
|
:param field: ormar field to check with choices
|
||||||
:type field: BaseField
|
:type field: BaseField
|
||||||
:param values: current values of the model to verify
|
:param value: current values of the model to verify
|
||||||
:type values: Dict
|
:type value: Dict
|
||||||
:return: value, choices list
|
:return: value, choices list
|
||||||
:rtype: Tuple[Any, List]
|
:rtype: Tuple[Any, List]
|
||||||
"""
|
"""
|
||||||
@ -97,6 +97,8 @@ def validate_choices(field: "BaseField", value: Any) -> None:
|
|||||||
:type value: Any
|
:type value: Any
|
||||||
"""
|
"""
|
||||||
value, choices = convert_choices_if_needed(field=field, value=value)
|
value, choices = convert_choices_if_needed(field=field, value=value)
|
||||||
|
if field.nullable:
|
||||||
|
choices.append(None)
|
||||||
if value is not ormar.Undefined and value not in choices:
|
if value is not ormar.Undefined and value not in choices:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"{field.name}: '{value}' " f"not in allowed choices set:" f" {choices}"
|
f"{field.name}: '{value}' " f"not in allowed choices set:" f" {choices}"
|
||||||
|
|||||||
@ -134,6 +134,26 @@ class Country(ormar.Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NullableCountry(ormar.Model):
|
||||||
|
class Meta:
|
||||||
|
tablename = "country2"
|
||||||
|
metadata = metadata
|
||||||
|
database = database
|
||||||
|
|
||||||
|
id: int = ormar.Integer(primary_key=True)
|
||||||
|
name: str = ormar.String(max_length=9, choices=country_name_choices, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
|
class NotNullableCountry(ormar.Model):
|
||||||
|
class Meta:
|
||||||
|
tablename = "country3"
|
||||||
|
metadata = metadata
|
||||||
|
database = database
|
||||||
|
|
||||||
|
id: int = ormar.Integer(primary_key=True)
|
||||||
|
name: str = ormar.String(max_length=9, choices=country_name_choices, nullable=False)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
def create_test_database():
|
def create_test_database():
|
||||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||||
@ -538,6 +558,17 @@ async def test_model_choices():
|
|||||||
await Country.objects.filter(name="Belize").update(name="Vietnam")
|
await Country.objects.filter(name="Belize").update(name="Vietnam")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_nullable_field_model_choices():
|
||||||
|
"""Test that choices work properly for according to nullable setting"""
|
||||||
|
async with database:
|
||||||
|
c1 = await NullableCountry(name=None).save()
|
||||||
|
assert c1.name is None
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
await NotNullableCountry(name=None).save()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_start_and_end_filters():
|
async def test_start_and_end_filters():
|
||||||
async with database:
|
async with database:
|
||||||
|
|||||||
Reference in New Issue
Block a user