There's a small bug in RawQuerySet. It breaks (returns a deferred model
instance instead of a regular model instance even when the appropriate
column is SELECTed) with models that have a ForeignKey with a db_column
specified. For example:

created_by = ForeignKey('User', db_column='created_by')

Here's the fix:

--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -1326,7 +1326,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:


A few lines above, model_init_kwargs is populated like this:
    model_init_kwargs[self.model_fields[column]]

model_fields is a property implemented by a method, which does this:
            for field in self.model._meta.fields:
                name, column = field.get_attname_column()
                self._model_fields[converter(column)] = name

It'd be clearer if the name variable in those two lines was changed to
attname, since that's what it is. Field.get_attname_column looks like
this:
    def get_attname_column(self):
        attname = self.get_attname()
        column = self.db_column or attname
        return attname, column

Richard

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to