diff --git a/django/db/models/query.py b/django/db/models/query.py index f301753e12..ab6a14e48a 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1402,7 +1402,7 @@ class RawQuerySet(object): # Construct model instance and apply annotations skip = set() for field in self.model._meta.fields: - if field.name not in model_init_kwargs.keys(): + if field.attname not in model_init_kwargs.keys(): skip.add(field.attname) if skip: diff --git a/tests/modeltests/raw_query/models.py b/tests/modeltests/raw_query/models.py index fb5503df3d..6188d58cb3 100644 --- a/tests/modeltests/raw_query/models.py +++ b/tests/modeltests/raw_query/models.py @@ -22,4 +22,7 @@ class Coffee(models.Model): brand = models.CharField(max_length=255, db_column="name") class Reviewer(models.Model): - reviewed = models.ManyToManyField(Book) \ No newline at end of file + reviewed = models.ManyToManyField(Book) + +class FriendlyAuthor(Author): + pass diff --git a/tests/modeltests/raw_query/tests.py b/tests/modeltests/raw_query/tests.py index ceed67473f..447849abb9 100644 --- a/tests/modeltests/raw_query/tests.py +++ b/tests/modeltests/raw_query/tests.py @@ -1,7 +1,10 @@ -from django.test import TestCase -from datetime import datetime -from models import Author, Book, Coffee, Reviewer +from datetime import date + from django.db.models.sql.query import InvalidQuery +from django.test import TestCase + +from models import Author, Book, Coffee, Reviewer, FriendlyAuthor + class RawQueryTests(TestCase): @@ -197,3 +200,13 @@ class RawQueryTests(TestCase): self.assertEquals(len(first_two), 2) self.assertRaises(TypeError, lambda: Author.objects.raw(query)['test']) + + def test_inheritance(self): + # date is the end of the Cuban Missile Crisis, I have no idea when + # Wesley was bron + f = FriendlyAuthor.objects.create(first_name="Wesley", last_name="Chun", + dob=date(1962, 10, 28)) + query = "SELECT * FROM raw_query_friendlyauthor" + self.assertEqual( + [o.pk for o in FriendlyAuthor.objects.raw(query)], [f.pk] + )