Python django.db.models.query_utils.InvalidQuery() Examples

The following are 11 code examples of django.db.models.query_utils.InvalidQuery(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db.models.query_utils , or try the search function .
Example #1
Source File: query.py    From bioforum with MIT License 5 votes vote down vote up
def __iter__(self):
        # Cache some things for performance reasons outside the loop.
        db = self.db
        compiler = connections[db].ops.compiler('SQLCompiler')(
            self.query, connections[db], db
        )

        query = iter(self.query)

        try:
            model_init_names, model_init_pos, annotation_fields = self.resolve_model_init_order()
            if self.model._meta.pk.attname not in model_init_names:
                raise InvalidQuery('Raw query must include the primary key')
            model_cls = self.model
            fields = [self.model_fields.get(c) for c in self.columns]
            converters = compiler.get_converters([
                f.get_col(f.model._meta.db_table) if f else None for f in fields
            ])
            if converters:
                query = compiler.apply_converters(query, converters)
            for values in query:
                # Associate fields to values
                model_init_values = [values[pos] for pos in model_init_pos]
                instance = model_cls.from_db(db, model_init_names, model_init_values)
                if annotation_fields:
                    for column, pos in annotation_fields:
                        setattr(instance, column, values[pos])
                yield instance
        finally:
            # Done iterating the Query. If it has its own cursor, close it.
            if hasattr(self.query, 'cursor') and self.query.cursor:
                self.query.cursor.close() 
Example #2
Source File: query.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def iterator(self):
        # Cache some things for performance reasons outside the loop.
        db = self.db
        compiler = connections[db].ops.compiler('SQLCompiler')(
            self.query, connections[db], db
        )

        query = iter(self.query)

        try:
            model_init_names, model_init_pos, annotation_fields = self.resolve_model_init_order()
            if self.model._meta.pk.attname not in model_init_names:
                raise InvalidQuery('Raw query must include the primary key')
            model_cls = self.model
            fields = [self.model_fields.get(c) for c in self.columns]
            converters = compiler.get_converters([
                f.get_col(f.model._meta.db_table) if f else None for f in fields
            ])
            if converters:
                query = compiler.apply_converters(query, converters)
            for values in query:
                # Associate fields to values
                model_init_values = [values[pos] for pos in model_init_pos]
                instance = model_cls.from_db(db, model_init_names, model_init_values)
                if annotation_fields:
                    for column, pos in annotation_fields:
                        setattr(instance, column, values[pos])
                yield instance
        finally:
            # Done iterating the Query. If it has its own cursor, close it.
            if hasattr(self.query, 'cursor') and self.query.cursor:
                self.query.cursor.close() 
Example #3
Source File: query.py    From python with Apache License 2.0 5 votes vote down vote up
def __iter__(self):
        # Cache some things for performance reasons outside the loop.
        db = self.db
        compiler = connections[db].ops.compiler('SQLCompiler')(
            self.query, connections[db], db
        )

        query = iter(self.query)

        try:
            model_init_names, model_init_pos, annotation_fields = self.resolve_model_init_order()

            # Find out which model's fields are not present in the query.
            skip = set()
            for field in self.model._meta.fields:
                if field.attname not in model_init_names:
                    skip.add(field.attname)
            if skip:
                if self.model._meta.pk.attname in skip:
                    raise InvalidQuery('Raw query must include the primary key')
            model_cls = self.model
            fields = [self.model_fields.get(c) for c in self.columns]
            converters = compiler.get_converters([
                f.get_col(f.model._meta.db_table) if f else None for f in fields
            ])
            for values in query:
                if converters:
                    values = compiler.apply_converters(values, converters)
                # Associate fields to values
                model_init_values = [values[pos] for pos in model_init_pos]
                instance = model_cls.from_db(db, model_init_names, model_init_values)
                if annotation_fields:
                    for column, pos in annotation_fields:
                        setattr(instance, column, values[pos])
                yield instance
        finally:
            # Done iterating the Query. If it has its own cursor, close it.
            if hasattr(self.query, 'cursor') and self.query.cursor:
                self.query.cursor.close() 
Example #4
Source File: query.py    From python2017 with MIT License 5 votes vote down vote up
def __iter__(self):
        # Cache some things for performance reasons outside the loop.
        db = self.db
        compiler = connections[db].ops.compiler('SQLCompiler')(
            self.query, connections[db], db
        )

        query = iter(self.query)

        try:
            model_init_names, model_init_pos, annotation_fields = self.resolve_model_init_order()

            # Find out which model's fields are not present in the query.
            skip = set()
            for field in self.model._meta.fields:
                if field.attname not in model_init_names:
                    skip.add(field.attname)
            if skip:
                if self.model._meta.pk.attname in skip:
                    raise InvalidQuery('Raw query must include the primary key')
            model_cls = self.model
            fields = [self.model_fields.get(c) for c in self.columns]
            converters = compiler.get_converters([
                f.get_col(f.model._meta.db_table) if f else None for f in fields
            ])
            for values in query:
                if converters:
                    values = compiler.apply_converters(values, converters)
                # Associate fields to values
                model_init_values = [values[pos] for pos in model_init_pos]
                instance = model_cls.from_db(db, model_init_names, model_init_values)
                if annotation_fields:
                    for column, pos in annotation_fields:
                        setattr(instance, column, values[pos])
                yield instance
        finally:
            # Done iterating the Query. If it has its own cursor, close it.
            if hasattr(self.query, 'cursor') and self.query.cursor:
                self.query.cursor.close() 
Example #5
Source File: tests.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_missing_fields_without_PK(self):
        query = "SELECT first_name, dob FROM raw_query_author"
        with self.assertRaisesMessage(InvalidQuery, 'Raw query must include the primary key'):
            list(Author.objects.raw(query)) 
Example #6
Source File: tests.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_defer_select_related_raises_invalid_query(self):
        msg = (
            'Field Primary.related cannot be both deferred and traversed '
            'using select_related at the same time.'
        )
        with self.assertRaisesMessage(InvalidQuery, msg):
            Primary.objects.defer("related").select_related("related")[0] 
Example #7
Source File: tests.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_only_select_related_raises_invalid_query(self):
        msg = (
            'Field Primary.related cannot be both deferred and traversed using '
            'select_related at the same time.'
        )
        with self.assertRaisesMessage(InvalidQuery, msg):
            Primary.objects.only("name").select_related("related")[0] 
Example #8
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_missing_fields_without_PK(self):
        query = "SELECT first_name, dob FROM raw_query_author"
        with self.assertRaisesMessage(InvalidQuery, 'Raw query must include the primary key'):
            list(Author.objects.raw(query)) 
Example #9
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_defer_select_related_raises_invalid_query(self):
        msg = (
            'Field Primary.related cannot be both deferred and traversed '
            'using select_related at the same time.'
        )
        with self.assertRaisesMessage(InvalidQuery, msg):
            Primary.objects.defer("related").select_related("related")[0] 
Example #10
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_only_select_related_raises_invalid_query(self):
        msg = (
            'Field Primary.related cannot be both deferred and traversed using '
            'select_related at the same time.'
        )
        with self.assertRaisesMessage(InvalidQuery, msg):
            Primary.objects.only("name").select_related("related")[0] 
Example #11
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_missing_fields_without_PK(self):
        query = "SELECT first_name, dob FROM raw_query_author"
        with self.assertRaisesMessage(InvalidQuery, 'Raw query must include the primary key'):
            list(Author.objects.raw(query))