diff --git a/docs_src/models/docs005.py b/docs_src/models/docs005.py index a95d477d5..0d309eb5e 100644 --- a/docs_src/models/docs005.py +++ b/docs_src/models/docs005.py @@ -36,7 +36,6 @@ class Course(ormar.Model): 'sql_nullable': False, 'index': False, 'unique': False, - 'choices': False, 'virtual': None, 'is_multi': None, 'is_relation': None, @@ -79,7 +78,6 @@ class Course(ormar.Model): 'sql_nullable': False, 'index': False, 'unique': False, - 'choices': False, 'virtual': None, 'is_multi': None, 'is_relation': None, @@ -120,7 +118,6 @@ class Course(ormar.Model): 'sql_nullable': True, 'index': False, 'unique': False, - 'choices': False, 'virtual': None, 'is_multi': None, 'is_relation': None, diff --git a/docs_src/queries/docs002.py b/docs_src/queries/docs002.py index 55117053b..1c3306819 100644 --- a/docs_src/queries/docs002.py +++ b/docs_src/queries/docs002.py @@ -23,7 +23,6 @@ class Book(ormar.Model): genre: str = ormar.String( max_length=100, default="Fiction", - choices=["Fiction", "Adventure", "Historic", "Fantasy"], ) diff --git a/docs_src/queries/docs003.py b/docs_src/queries/docs003.py index a0f310a71..09b928e75 100644 --- a/docs_src/queries/docs003.py +++ b/docs_src/queries/docs003.py @@ -21,7 +21,6 @@ class Book(ormar.Model): genre: str = ormar.String( max_length=100, default="Fiction", - choices=["Fiction", "Adventure", "Historic", "Fantasy"], ) diff --git a/docs_src/queries/docs005.py b/docs_src/queries/docs005.py index 3145b9216..ff9a3606f 100644 --- a/docs_src/queries/docs005.py +++ b/docs_src/queries/docs005.py @@ -21,7 +21,6 @@ class Book(ormar.Model): genre: str = ormar.String( max_length=100, default="Fiction", - choices=["Fiction", "Adventure", "Historic", "Fantasy"], ) diff --git a/ormar/fields/base.py b/ormar/fields/base.py index 297f83abd..a55f9a956 100644 --- a/ormar/fields/base.py +++ b/ormar/fields/base.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Type, Union +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union import sqlalchemy from pydantic.fields import FieldInfo, _Unset @@ -43,7 +43,6 @@ def __init__(self, **kwargs: Any) -> None: self.sql_nullable: bool = kwargs.pop("sql_nullable", False) self.index: bool = kwargs.pop("index", False) self.unique: bool = kwargs.pop("unique", False) - self.choices: Sequence = kwargs.pop("choices", False) self.virtual: bool = kwargs.pop( "virtual", None diff --git a/ormar/fields/parsers.py b/ormar/fields/parsers.py index 4e29686e2..9b73304fc 100644 --- a/ormar/fields/parsers.py +++ b/ormar/fields/parsers.py @@ -53,10 +53,10 @@ def encode_json(value: Any) -> Optional[str]: def re_dump_value(value: str) -> Union[str, bytes]: """ - Rw-dumps choices due to different string representation in orjson and json + Re-dumps value due to different string representation in orjson and json :param value: string to re-dump :type value: str - :return: re-dumped choices + :return: re-dumped value :rtype: List[str] """ try: diff --git a/ormar/models/helpers/validation.py b/ormar/models/helpers/validation.py index 73e49d839..f284ce8b9 100644 --- a/ormar/models/helpers/validation.py +++ b/ormar/models/helpers/validation.py @@ -201,7 +201,7 @@ def overwrite_binary_format(schema: Dict[str, Any], model: Type["Model"]) -> Non prop["format"] = "base64" -def construct_schema_function_without_choices() -> Callable: +def construct_schema_function() -> Callable: """ Modifies model example and description if needed. @@ -221,13 +221,10 @@ def schema_extra(schema: Dict[str, Any], model: Type["Model"]) -> None: def modify_schema_example(model: Type["Model"]) -> None: # noqa CCR001 """ - Checks if Model has any fields with choices set. - If yes it adds choices validation into pre root validators. + Modifies the schema example in openapi schema. :param model: newly constructed Model :type model: Model class """ if not config_field_not_set(model=model, field_name="model_fields"): - model.model_config["json_schema_extra"] = ( - construct_schema_function_without_choices() - ) + model.model_config["json_schema_extra"] = construct_schema_function() diff --git a/ormar/models/mixins/save_mixin.py b/ormar/models/mixins/save_mixin.py index 25e87c2d2..e75949929 100644 --- a/ormar/models/mixins/save_mixin.py +++ b/ormar/models/mixins/save_mixin.py @@ -238,7 +238,7 @@ def populate_default_values(cls, new_kwargs: Dict) -> Dict: return new_kwargs @classmethod - def validate_choices(cls, new_kwargs: Dict) -> Dict: + def validate_enums(cls, new_kwargs: Dict) -> Dict: """ Receives dictionary of model that is about to be saved and validates the fields with choices set to see if the value is allowed. diff --git a/ormar/models/quick_access_views.py b/ormar/models/quick_access_views.py index a44c08f08..17cadb778 100644 --- a/ormar/models/quick_access_views.py +++ b/ormar/models/quick_access_views.py @@ -21,7 +21,6 @@ "__private_attributes__", "__same__", "_calculate_keys", - "_choices_fields", "_convert_json", "_extract_db_related_names", "_extract_model_db_fields", diff --git a/ormar/queryset/queryset.py b/ormar/queryset/queryset.py index 14270c65d..84657fe84 100644 --- a/ormar/queryset/queryset.py +++ b/ormar/queryset/queryset.py @@ -801,7 +801,7 @@ async def update(self, each: bool = False, **kwargs: Any) -> int: self.model.extract_related_names() ) updates = {k: v for k, v in kwargs.items() if k in self_fields} - updates = self.model.validate_choices(updates) + updates = self.model.validate_enums(updates) updates = self.model.translate_columns_to_aliases(updates) expr = FilterQuery(filter_clauses=self.filter_clauses).apply( diff --git a/tests/test_model_definition/test_models.py b/tests/test_model_definition/test_models.py index 452696e36..df35bff27 100644 --- a/tests/test_model_definition/test_models.py +++ b/tests/test_model_definition/test_models.py @@ -44,7 +44,7 @@ class LargeBinaryStr(ormar.Model): id: int = ormar.Integer(primary_key=True) test_binary: str = ormar.LargeBinary( - max_length=100000, choices=[blob3, blob4], represent_as_base64_str=True + max_length=100000, represent_as_base64_str=True ) @@ -54,7 +54,6 @@ class LargeBinaryNullableStr(ormar.Model): id: int = ormar.Integer(primary_key=True) test_binary: str = ormar.LargeBinary( max_length=100000, - choices=[blob3, blob4], represent_as_base64_str=True, nullable=True, ) @@ -466,9 +465,9 @@ async def test_model_first(): @pytest.mark.asyncio async def test_model_choices(): - """Test that choices work properly for various types of fields.""" + """Test that enum work properly for various types of fields.""" async with base_ormar_config.database: - # Test valid choices. + # Test valid enums values. await asyncio.gather( Country.objects.create(name="Canada", taxed=True, country_code=1), Country.objects.create(name="Algeria", taxed=True, country_code=213), @@ -505,8 +504,8 @@ async def test_model_choices(): @pytest.mark.asyncio -async def test_nullable_field_model_choices(): - """Test that choices work properly for according to nullable setting""" +async def test_nullable_field_model_enum(): + """Test that enum work properly for according to nullable setting""" async with base_ormar_config.database: c1 = await NullableCountry(name=None).save() assert c1.name is None diff --git a/tests/test_queries/test_queryset_level_methods.py b/tests/test_queries/test_queryset_level_methods.py index f223eb221..801b028f0 100644 --- a/tests/test_queries/test_queryset_level_methods.py +++ b/tests/test_queries/test_queryset_level_methods.py @@ -32,7 +32,6 @@ class Book(ormar.Model): genre: str = ormar.String( max_length=100, default="Fiction", - choices=["Fiction", "Adventure", "Historic", "Fantasy"], ) diff --git a/tests/test_relations/test_foreign_keys.py b/tests/test_relations/test_foreign_keys.py index e77d9a1f1..49c1ae5b6 100644 --- a/tests/test_relations/test_foreign_keys.py +++ b/tests/test_relations/test_foreign_keys.py @@ -41,7 +41,7 @@ class Organisation(ormar.Model): ormar_config = base_ormar_config.copy(tablename="org") id: int = ormar.Integer(primary_key=True) - ident: str = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"]) + ident: str = ormar.String(max_length=100) class Team(ormar.Model): diff --git a/tests/test_relations/test_postgress_select_related_with_limit.py b/tests/test_relations/test_postgress_select_related_with_limit.py index a12d5b009..14ff84964 100644 --- a/tests/test_relations/test_postgress_select_related_with_limit.py +++ b/tests/test_relations/test_postgress_select_related_with_limit.py @@ -27,9 +27,7 @@ class User(PrimaryKeyMixin, ormar.Model): mobile: str = ormar.String(unique=True, index=True, max_length=10) password: str = ormar.String(max_length=128) - level: str = ormar.String( - max_length=1, choices=list(Level), default=Level.STAFF.value - ) + level: Level = ormar.Enum(default=Level.STAFF, enum_class=Level) email: Optional[str] = ormar.String(max_length=255, nullable=True, default=None) avatar: Optional[str] = ormar.String(max_length=255, nullable=True, default=None) fullname: Optional[str] = ormar.String(max_length=64, nullable=True, default=None)