clean conflicts and some cleaning

This commit is contained in:
collerek
2020-08-19 19:23:04 +07:00
6 changed files with 7 additions and 26 deletions

View File

@ -111,7 +111,6 @@ class ForeignKeyField(BaseField):
def expand_relationship( def expand_relationship(
cls, value: Any, child: "Model" cls, value: Any, child: "Model"
) -> Optional[Union["Model", List["Model"]]]: ) -> Optional[Union["Model", List["Model"]]]:
print("expandong relatiknship", value, child)
if value is None: if value is None:
return None return None

View File

@ -171,9 +171,7 @@ class FakePydantic(pydantic.BaseModel, metaclass=ModelMetaclass):
exclude_defaults: bool = False, exclude_defaults: bool = False,
exclude_none: bool = False, exclude_none: bool = False,
nested: bool = False nested: bool = False
) -> 'DictStrAny': # noqa: A003 ) -> 'DictStrAny': # noqa: A003'
print('callin super', self.__class__)
print('to exclude', self._exclude_related_names_not_required(nested))
dict_instance = super().dict(include=include, dict_instance = super().dict(include=include,
exclude=self._exclude_related_names_not_required(nested), exclude=self._exclude_related_names_not_required(nested),
by_alias=by_alias, by_alias=by_alias,
@ -181,9 +179,7 @@ class FakePydantic(pydantic.BaseModel, metaclass=ModelMetaclass):
exclude_unset=exclude_unset, exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults, exclude_defaults=exclude_defaults,
exclude_none=exclude_none) exclude_none=exclude_none)
print('after super')
for field in self._extract_related_names(): for field in self._extract_related_names():
print(self.__class__, field, nested)
nested_model = getattr(self, field) nested_model = getattr(self, field)
if self.Meta.model_fields[field].virtual and nested: if self.Meta.model_fields[field].virtual and nested:
@ -191,13 +187,9 @@ class FakePydantic(pydantic.BaseModel, metaclass=ModelMetaclass):
if isinstance(nested_model, list) and not isinstance( if isinstance(nested_model, list) and not isinstance(
nested_model, ormar.Model nested_model, ormar.Model
): ):
print('nested list')
dict_instance[field] = [x.dict(nested=True) for x in nested_model] dict_instance[field] = [x.dict(nested=True) for x in nested_model]
else: elif nested_model is not None:
print('instance') dict_instance[field] = nested_model.dict(nested=True)
if nested_model is not None:
dict_instance[field] = nested_model.dict(nested=True)
return dict_instance return dict_instance
def from_dict(self, value_dict: Dict) -> None: def from_dict(self, value_dict: Dict) -> None:

View File

@ -113,11 +113,9 @@ def populate_pydantic_default_values(attrs: Dict) -> Dict:
type_.name = field type_.name = field
def_value = type_.default_value() def_value = type_.default_value()
curr_def_value = attrs.get(field, 'NONE') curr_def_value = attrs.get(field, 'NONE')
print(field, curr_def_value, 'def val', type_.nullable)
if curr_def_value == 'NONE' and isinstance(def_value, FieldInfo): if curr_def_value == 'NONE' and isinstance(def_value, FieldInfo):
attrs[field] = def_value attrs[field] = def_value
elif curr_def_value == 'NONE' and type_.nullable: elif curr_def_value == 'NONE' and type_.nullable:
print(field, 'defsults tp none')
attrs[field] = FieldInfo(default=None) attrs[field] = FieldInfo(default=None)
return attrs return attrs
@ -148,8 +146,6 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
attrs["__annotations__"]= annotations attrs["__annotations__"]= annotations
attrs = populate_pydantic_default_values(attrs) attrs = populate_pydantic_default_values(attrs)
print(attrs)
tablename = name.lower() + "s" tablename = name.lower() + "s"
new_model.Meta.tablename = new_model.Meta.tablename or tablename new_model.Meta.tablename = new_model.Meta.tablename or tablename
@ -182,8 +178,6 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
) )
new_model.Meta.model_fields = model_fields new_model.Meta.model_fields = model_fields
print(attrs, 'before super')
print(new_model.Meta.__dict__)
new_model = super().__new__( # type: ignore new_model = super().__new__( # type: ignore
mcs, name, bases, attrs mcs, name, bases, attrs
) )

View File

@ -79,7 +79,7 @@ class Query:
expr = self._apply_expression_modifiers(expr) expr = self._apply_expression_modifiers(expr)
print(expr.compile(compile_kwargs={"literal_binds": True})) #print(expr.compile(compile_kwargs={"literal_binds": True}))
self._reset_query_parameters() self._reset_query_parameters()
return expr, self._select_related return expr, self._select_related
@ -179,7 +179,6 @@ class Query:
): ):
rel_part = field_name if not rel_part else rel_part + "__" + field_name rel_part = field_name if not rel_part else rel_part + "__" + field_name
if not field.nullable: if not field.nullable:
print('add', rel_part, field)
if rel_part not in self._select_related: if rel_part not in self._select_related:
new_related = "__".join(rel_part.split("__")[:-1]) if len( new_related = "__".join(rel_part.split("__")[:-1]) if len(
rel_part.split("__")) > 1 else rel_part rel_part.split("__")) > 1 else rel_part
@ -188,7 +187,7 @@ class Query:
elif self._field_qualifies_to_deeper_search( elif self._field_qualifies_to_deeper_search(
field, parent_virtual, nested, rel_part field, parent_virtual, nested, rel_part
): ):
print('deeper', rel_part, field, field.to)
self._extract_auto_required_relations( self._extract_auto_required_relations(
prev_model=field.to, prev_model=field.to,
rel_part=rel_part, rel_part=rel_part,

View File

@ -69,10 +69,8 @@ class RelationshipManager:
@staticmethod @staticmethod
def append_related_model(relations_list: List["Model"], model: "Model") -> None: def append_related_model(relations_list: List["Model"], model: "Model") -> None:
print("appending", relations_list, model)
for relation_child in relations_list: for relation_child in relations_list:
try: try:
print(relation_child.__same__(model), "same")
if relation_child.__same__(model): if relation_child.__same__(model):
return return
except ReferenceError: except ReferenceError:

View File

@ -118,15 +118,14 @@ async def test_model_crud():
await track2.save() await track2.save()
await track3.save() await track3.save()
assert len(album.tracks) == 3
assert album.tracks[1].title == "Heart don't stand a chance"
track = await Track.objects.get(title="The Bird") track = await Track.objects.get(title="The Bird")
assert track.album.pk == album.pk assert track.album.pk == album.pk
assert track.album.name is None assert track.album.name is None
await track.album.load() await track.album.load()
assert track.album.name == "Malibu" assert track.album.name == "Malibu"
assert len(album.tracks) == 3
assert album.tracks[1].title == "Heart don't stand a chance"
album1 = await Album.objects.get(name="Malibu") album1 = await Album.objects.get(name="Malibu")
assert album1.pk == 1 assert album1.pk == 1