bump version, update realease, convert enums to vals

This commit is contained in:
collerek
2021-02-03 14:26:40 +01:00
parent a028e96f3e
commit 867d480728
7 changed files with 89 additions and 25 deletions

View File

@ -145,6 +145,49 @@ Sample:
When loaded it's always python UUID so you can compare it and compare two formats values between each other.
### Enum
Although there is no dedicated field type for Enums in `ormar` you can change any
field into `Enum` like field by passing a `choices` list that is accepted by all Field types.
It will add both: validation in `pydantic` model and will display available options in schema,
therefore it will be available in docs of `fastapi`.
If you still want to use `Enum` in your application you can do this by passing a `Enum` into choices
and later pass value of given option to a given field (note tha Enum is not JsonSerializable).
```python
# not that imports and endpoints declaration
# is skipped here for brevity
from enum import Enum
class TestEnum(Enum):
val1 = 'Val1'
val2 = 'Val2'
class TestModel(ormar.Model):
class Meta:
tablename = "org"
metadata = metadata
database = database
id: int = ormar.Integer(primary_key=True)
# pass list(Enum) to choices
enum_string: str = ormar.String(max_length=100, choices=list(TestEnum))
# sample payload coming to fastapi
response = client.post(
"/test_models/",
json={
"id": 1,
# you need to refer to the value of the `Enum` option
# if called like this, alternatively just use value
# string "Val1" in this case
"enum_string": TestEnum.val1.value
},
)
```
[relations]: ../relations/index.md
[queries]: ../queries.md
[pydantic]: https://pydantic-docs.helpmanual.io/usage/types/#constrained-types

View File

@ -187,6 +187,7 @@ Available Model Fields (with required args - optional ones in docs):
* `BigInteger()`
* `Decimal(scale, precision)`
* `UUID()`
* `EnumField` - by passing `choices` to any other Field type
* `ForeignKey(to)`
* `ManyToMany(to, through)`

View File

@ -1,3 +1,11 @@
# 0.9.1
## Features
* Add choices values to `OpenAPI` specs, so it looks like native `Enum` field in the result schema.
## Fixes
* Fix `choices` behavior with `fastapi` usage when special fields can be not initialized yet but passed as strings etc.
# 0.9.0
## Important