fix qryset fields, model update, model delete, model load, qryset update, qruset filter, qryset bulk_load, qryset bulk_update

This commit is contained in:
collerek
2020-10-21 12:14:14 +02:00
parent 64fd9f3cce
commit dbca4367e8
7 changed files with 79 additions and 14 deletions

View File

@ -83,7 +83,7 @@ class QueryClause:
else:
op = "exact"
column = self.table.columns[key]
column = self.table.columns[self.model_cls.get_column_alias(key)]
table = self.table
clause = self._process_column_clause_for_operator_and_value(

View File

@ -110,7 +110,7 @@ class SqlJoin:
pkname_alias = model_cls.get_column_alias(model_cls.Meta.pkname)
self.order_bys.append(text(f"{alias}_{to_table}.{pkname_alias}"))
self_related_fields = model_cls.own_table_columns(
model_cls, self.fields, nested=True
model_cls, self.fields, nested=True,
)
self.columns.extend(
self.relation_manager(model_cls).prefixed_columns(

View File

@ -93,6 +93,12 @@ class QuerySet:
new_kwargs[field.name] = new_kwargs.pop(field_name)
return new_kwargs
def _translate_aliases_to_columns(self, new_kwargs: dict) -> dict:
for field_name, field in self.model_meta.model_fields.items():
if field.name in new_kwargs and field.name != field_name:
new_kwargs[field_name] = new_kwargs.pop(field.name)
return new_kwargs
def _remove_pk_from_kwargs(self, new_kwargs: dict) -> dict:
pkname = self.model_meta.pkname
pk = self.model_meta.model_fields[pkname]
@ -201,6 +207,7 @@ class QuerySet:
async def update(self, each: bool = False, **kwargs: Any) -> int:
self_fields = self.model.extract_db_own_fields()
updates = {k: v for k, v in kwargs.items() if k in self_fields}
updates = self._translate_columns_to_aliases(updates)
if not each and not self.filter_clauses:
raise QueryDefinitionError(
"You cannot update without filtering the queryset first. "
@ -336,6 +343,8 @@ class QuerySet:
if pk_name not in columns:
columns.append(pk_name)
columns = [self.model.get_column_alias(k) for k in columns]
for objt in objects:
new_kwargs = objt.dict()
if pk_name not in new_kwargs or new_kwargs.get(pk_name) is None:
@ -344,13 +353,22 @@ class QuerySet:
f"{self.model.__name__} has to have {pk_name} filled."
)
new_kwargs = self.model.substitute_models_with_pks(new_kwargs)
new_kwargs = self._translate_columns_to_aliases(new_kwargs)
new_kwargs = {"new_" + k: v for k, v in new_kwargs.items() if k in columns}
ready_objects.append(new_kwargs)
pk_column = self.model_meta.table.c.get(pk_name)
expr = self.table.update().where(pk_column == bindparam("new_" + pk_name))
pk_column_name = self.model.get_column_alias(pk_name)
table_columns = [c.name for c in self.model_meta.table.c]
expr = self.table.update().where(
pk_column == bindparam("new_" + pk_column_name)
)
expr = expr.values(
**{k: bindparam("new_" + k) for k in columns if k != pk_name}
**{
k: bindparam("new_" + k)
for k in columns
if k != pk_column_name and k in table_columns
}
)
# databases bind params only where query is passed as string
# otherwise it just pases all data to values and results in unconsumed columns