revert adding props to fields
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import inspect
|
||||
import logging
|
||||
import warnings
|
||||
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Tuple, Type, Union
|
||||
@ -335,10 +336,9 @@ def populate_default_options_values(
|
||||
|
||||
def add_cached_properties(new_model: Type["Model"]) -> None:
|
||||
new_model._props = {
|
||||
prop
|
||||
for prop in vars(new_model)
|
||||
if isinstance(getattr(new_model, prop), property)
|
||||
and prop not in ("__values__", "__fields__", "fields", "pk_column", "saved")
|
||||
prop[0]
|
||||
for prop in inspect.getmembers(new_model, lambda o: isinstance(o, property))
|
||||
if prop[0] not in ("__values__", "__fields__", "fields", "pk_column", "saved")
|
||||
}
|
||||
new_model._quick_access_fields = quick_access_set
|
||||
new_model._related_names = None
|
||||
@ -346,19 +346,22 @@ def add_cached_properties(new_model: Type["Model"]) -> None:
|
||||
|
||||
|
||||
def add_property_fields(new_model: Type["Model"]) -> None:
|
||||
if new_model.Meta.include_props_in_fields:
|
||||
for prop in new_model._props:
|
||||
field_type = getattr(new_model, prop).fget.__annotations__.get("return")
|
||||
new_model.Meta.model_fields[prop] = ModelFieldFactory( # type: ignore
|
||||
nullable=True, pydantic_only=True
|
||||
)
|
||||
new_model.__fields__[prop] = ModelField(
|
||||
name=prop,
|
||||
type_=Optional[field_type] if field_type is not None else Any, # type: ignore
|
||||
model_config=new_model.__config__,
|
||||
required=False,
|
||||
class_validators={},
|
||||
)
|
||||
pass
|
||||
|
||||
|
||||
# if new_model.Meta.include_props_in_fields:
|
||||
# for prop in new_model._props:
|
||||
# field_type = getattr(new_model, prop).fget.__annotations__.get("return")
|
||||
# new_model.Meta.model_fields[prop] = ModelFieldFactory( # type: ignore
|
||||
# nullable=True, pydantic_only=True
|
||||
# )
|
||||
# new_model.__fields__[prop] = ModelField(
|
||||
# name=prop,
|
||||
# type_=Optional[field_type] if field_type is not None else Any, # type: ignore
|
||||
# model_config=new_model.__config__,
|
||||
# required=False,
|
||||
# class_validators={},
|
||||
# )
|
||||
|
||||
|
||||
class ModelMetaclass(pydantic.main.ModelMetaclass):
|
||||
@ -371,6 +374,7 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
|
||||
new_model = super().__new__( # type: ignore
|
||||
mcs, name, bases, attrs
|
||||
)
|
||||
add_cached_properties(new_model)
|
||||
|
||||
if hasattr(new_model, "Meta"):
|
||||
populate_default_options_values(new_model, model_fields)
|
||||
@ -387,7 +391,6 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
|
||||
)
|
||||
new_model.Meta.alias_manager = alias_manager
|
||||
new_model.objects = QuerySet(new_model)
|
||||
add_cached_properties(new_model)
|
||||
add_property_fields(new_model)
|
||||
|
||||
return new_model
|
||||
|
||||
@ -20,7 +20,6 @@ from typing import (
|
||||
from ormar.exceptions import ModelPersistenceError, RelationshipInstanceError
|
||||
from ormar.queryset.utils import translate_list_to_dict, update
|
||||
|
||||
|
||||
import ormar # noqa: I100
|
||||
from ormar.fields import BaseField
|
||||
from ormar.fields.foreign_key import ForeignKeyField
|
||||
@ -46,13 +45,11 @@ class ModelTableProxy:
|
||||
pk: Any
|
||||
get_name: Callable
|
||||
_props: Set
|
||||
|
||||
def dict(self): # noqa A003
|
||||
raise NotImplementedError # pragma no cover
|
||||
dict: Callable
|
||||
|
||||
def _extract_own_model_fields(self) -> Dict:
|
||||
related_names = self.extract_related_names()
|
||||
self_fields = {k: v for k, v in self.dict().items() if k not in related_names}
|
||||
self_fields = self.dict(exclude=related_names)
|
||||
return self_fields
|
||||
|
||||
@classmethod
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import inspect
|
||||
import json
|
||||
import uuid
|
||||
from typing import (
|
||||
@ -74,7 +75,7 @@ class NewBaseModel(
|
||||
|
||||
# noinspection PyMissingConstructor
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None: # type: ignore
|
||||
|
||||
caller_name = inspect.currentframe().f_back.f_code.co_name
|
||||
object.__setattr__(self, "_orm_id", uuid.uuid4().hex)
|
||||
object.__setattr__(self, "_orm_saved", False)
|
||||
object.__setattr__(
|
||||
@ -96,6 +97,8 @@ class NewBaseModel(
|
||||
if "pk" in kwargs:
|
||||
kwargs[self.Meta.pkname] = kwargs.pop("pk")
|
||||
# build the models to set them and validate but don't register
|
||||
if self.Meta.include_props_in_dict:
|
||||
kwargs = {k: v for k, v in kwargs.items() if k not in object.__getattribute__(self, '_props')}
|
||||
try:
|
||||
new_kwargs: Dict[str, Any] = {
|
||||
k: self._convert_json(
|
||||
@ -312,6 +315,8 @@ class NewBaseModel(
|
||||
exclude_none: bool = False,
|
||||
nested: bool = False,
|
||||
) -> "DictStrAny": # noqa: A003'
|
||||
# callable_name = inspect.currentframe().f_back.f_code.co_name
|
||||
# print('dict', callable_name)
|
||||
dict_instance = super().dict(
|
||||
include=include,
|
||||
exclude=self._update_excluded_with_related_not_required(exclude, nested),
|
||||
|
||||
@ -64,7 +64,7 @@ class RandomModel(ormar.Model):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
include_props_in_fields = True
|
||||
include_props_in_dict = True
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
password: str = ormar.String(max_length=255, default=gen_pass)
|
||||
@ -139,7 +139,9 @@ async def create_user4(user: User2):
|
||||
|
||||
@app.post("/random/", response_model=RandomModel)
|
||||
async def create_user5(user: RandomModel):
|
||||
return await user.save()
|
||||
user = await user.save()
|
||||
print('returning')
|
||||
return user
|
||||
|
||||
|
||||
@app.post("/random2/", response_model=RandomModel)
|
||||
@ -148,7 +150,7 @@ async def create_user6(user: RandomModel):
|
||||
return user.dict()
|
||||
|
||||
|
||||
def test_all_endpoints():
|
||||
def test_excluding_fields_in_endpoints():
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
user = {
|
||||
@ -168,20 +170,24 @@ def test_all_endpoints():
|
||||
"last_name": "Doe",
|
||||
}
|
||||
|
||||
print('before call')
|
||||
response = client.post("/users/", json=user2)
|
||||
created_user = User(**response.json())
|
||||
assert created_user.pk is not None
|
||||
assert created_user.password is None
|
||||
|
||||
print('before call')
|
||||
response = client.post("/users2/", json=user)
|
||||
created_user2 = User(**response.json())
|
||||
assert created_user2.pk is not None
|
||||
assert created_user2.password is None
|
||||
|
||||
# response has only 3 fields from UserBase
|
||||
print('before call')
|
||||
response = client.post("/users3/", json=user)
|
||||
assert list(response.json().keys()) == ["email", "first_name", "last_name"]
|
||||
|
||||
print('before call')
|
||||
response = client.post("/users4/", json=user)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
@ -191,32 +197,40 @@ def test_all_endpoints():
|
||||
"category",
|
||||
]
|
||||
|
||||
user3 = {"last_name": "Test"}
|
||||
response = client.post("/random/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"password",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"created_date",
|
||||
"full_name",
|
||||
]
|
||||
assert response.json().get("full_name") == "John Test"
|
||||
# user3 = {"last_name": "Test"}
|
||||
# print('before call')
|
||||
# response = client.post("/random/", json=user3)
|
||||
# assert list(response.json().keys()) == [
|
||||
# "id",
|
||||
# "password",
|
||||
# "first_name",
|
||||
# "last_name",
|
||||
# "created_date",
|
||||
# "full_name",
|
||||
# ]
|
||||
# assert response.json().get("full_name") == "John Test"
|
||||
#
|
||||
# RandomModel.Meta.include_props_in_fields = False
|
||||
# user3 = {"last_name": "Test"}
|
||||
# print('before call')
|
||||
# response = client.post("/random/", json=user3)
|
||||
# assert list(response.json().keys()) == [
|
||||
# "id",
|
||||
# "password",
|
||||
# "first_name",
|
||||
# "last_name",
|
||||
# "created_date",
|
||||
# "full_name",
|
||||
# ]
|
||||
# assert response.json().get("full_name") == "John Test"
|
||||
|
||||
RandomModel.Meta.include_props_in_fields = False
|
||||
user3 = {"last_name": "Test"}
|
||||
response = client.post("/random/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"password",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"created_date",
|
||||
"full_name",
|
||||
]
|
||||
|
||||
def test_adding_fields_in_endpoints():
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
RandomModel.Meta.include_props_in_dict = True
|
||||
user3 = {"last_name": "Test"}
|
||||
print('before call')
|
||||
response = client.post("/random2/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
@ -226,3 +240,4 @@ def test_all_endpoints():
|
||||
"created_date",
|
||||
"full_name",
|
||||
]
|
||||
assert response.json().get("full_name") == "John Test"
|
||||
|
||||
@ -28,10 +28,6 @@ class Album(ormar.Model):
|
||||
def name10(self) -> str:
|
||||
return self.name + "_10"
|
||||
|
||||
@validator("name")
|
||||
def test(cls, v):
|
||||
return v
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
@ -71,5 +67,4 @@ async def test_pydantic_only_fields():
|
||||
test_dict = album.dict()
|
||||
assert "timestamp" in test_dict
|
||||
assert test_dict["timestamp"] is not None
|
||||
# key is still there as now it's a field
|
||||
assert test_dict["name10"] is None
|
||||
assert test_dict.get("name10", 'aa') == 'aa'
|
||||
|
||||
Reference in New Issue
Block a user