Esempio n. 1
0
    def check(self, against, using=DEFAULT_DB_ALIAS):
        """
        Do a database query to check if the expressions of the Q instance
        matches against the expressions.
        """
        # Avoid circular imports.
        from django.db.models import Value
        from django.db.models.sql import Query
        from django.db.models.sql.constants import SINGLE

        query = Query(None)
        for name, value in against.items():
            if not hasattr(value, "resolve_expression"):
                value = Value(value)
            query.add_annotation(value, name, select=False)
        query.add_annotation(Value(1), "_check")
        # This will raise a FieldError if a field is missing in "against".
        query.add_q(self)
        compiler = query.get_compiler(using=using)
        try:
            return compiler.execute_sql(SINGLE) is not None
        except DatabaseError as e:
            logger.warning("Got a database error calling check() on %r: %s",
                           self, e)
            return True
Esempio n. 2
0
 def _get_condition_sql(self, model, schema_editor):
     if self.condition is None:
         return ''
     query = Query(model=model)
     query.add_q(self.condition)
     compiler = query.get_compiler(connection=schema_editor.connection)
     # Only the WhereNode is of interest for the partial index.
     sql, params = query.where.as_sql(compiler=compiler, connection=schema_editor.connection)
     # BaseDatabaseSchemaEditor does the same map on the params, but since
     # it's handled outside of that class, the work is done here.
     return ' WHERE ' + (sql % tuple(map(schema_editor.quote_value, params)))
Esempio n. 3
0
 def _get_condition_sql(self, model, schema_editor):
     if self.condition is None:
         return ''
     query = Query(model=model)
     query.add_q(self.condition)
     compiler = query.get_compiler(connection=schema_editor.connection)
     # Only the WhereNode is of interest for the partial index.
     sql, params = query.where.as_sql(compiler=compiler, connection=schema_editor.connection)
     # BaseDatabaseSchemaEditor does the same map on the params, but since
     # it's handled outside of that class, the work is done here.
     return ' WHERE ' + (sql % tuple(map(schema_editor.quote_value, params)))
Esempio n. 4
0
 def _make_where(self, *args, **kwargs):
     q = Query(TestModel)
     for arg in args:
         q.add_q(arg)
     q.add_q(Q(**kwargs))
     return q.where