Esempio n. 1
0
    def resolve_into(self, queryset: "AwaitableQuery[MODEL]",
                     context: QueryContext):
        if isinstance(self.node, Term):
            term_annotation = TermAnnotation(self.node)
            term_annotation.resolve_into(queryset, context)

            field = term_annotation.field
            direction = Order.asc
            if isinstance(field, Negative):
                field = field.term
                direction = Order.desc

            context.query = context.query.orderby(field, order=direction)

        else:
            context.query = context.query.orderby(self.node)
Esempio n. 2
0
    def create_query(self, parent_context: Optional[QueryContext]) -> QueryBuilder:

        db_client = self._get_db_client()
        table = self.model._meta.table(parent_context.alias if parent_context else None)

        context = QueryContext(query=db_client.query_class.update(table), parent_context=parent_context)
        context.push(self.model, table)
        self._add_query_details(context=context)

        for field_name, value in self.update_kwargs.items():
            field_object = self.model._meta.fields_map.get(field_name)

            if not field_object:
                raise UnknownFieldError(field_name, self.model)

            if field_object.primary_key:
                raise IntegrityError(f"Field {field_name} is primary key and can not be updated")

            if isinstance(field_object, RelationField):
                if isinstance(field_object, (ForeignKey, OneToOneField)):
                    fk_field_name: str = field_object.id_field_name
                    fk_field_object = self.model._meta.fields_map[fk_field_name]
                    value = fk_field_object.db_value(value.pk, None)
                    context.query = context.query.set(fk_field_object.db_column, value)

                else:
                    raise NotADbColumnFieldError(field_name, self.model)

            else:
                if isinstance(value, Term):
                    value = TermAnnotation(value)
                    value.resolve_into(self, context)
                    value = value.field

                elif isinstance(value, Annotation):
                    value.resolve_into(self, context)
                    value = value.field

                else:
                    value = field_object.db_value(value, None)

                context.query = context.query.set(field_object.db_column, value)

        context.pop()
        return context.query
Esempio n. 3
0
    def resolve_into(self, queryset: "AwaitableStatement[MODEL]",
                     context: QueryContext):
        self._field_object, self._field = context.resolve_term(
            self._term, queryset, accept_relation=True)

        model = context.top.model
        table = context.top.table
        if self._add_group_by and self._field.is_aggregate:
            context.query = context.query.groupby(
                table[model._meta.pk_db_column])
Esempio n. 4
0
    def resolve_into(self, queryset: "AwaitableQuery[MODEL]",
                     context: QueryContext):
        # So far as I can imagine, the annotation will be expanded
        # independently, we just refer to it here.
        _, field = context.resolve_field_name(self.field_name,
                                              queryset,
                                              accept_relation=False,
                                              expand_annotation=False)

        if not queryset.is_aggregate() or context.query._groupbys:
            context.query = context.query.orderby(field, order=self.direction)