﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25563	Deferred models should be cached in their proxied model app	Andriy Sokolovskiy	Simon Charette	"Consider simple model and custom manager:


{{{
from django.db import models


class CustomManager(models.Manager):
    use_in_migrations = True

    def get_queryset(self, *args, **kwargs):
        return super(CustomManager, self).get_queryset(
            *args, **kwargs
        ).defer('test')


class ModelA(models.Model):
    test = models.CharField(blank=True, max_length=123)
    objects = CustomManager()

}}}

Also there is a simple test:

{{{
from django.test import TestCase
from .models import ModelA


class Test(TestCase):
    def test_defer_bug(self):
        ModelA.objects.create(test='test')
        self.assertIsInstance(ModelA.objects.last(), ModelA)
}}}

With initial migration test is passing well. But, when adding a datamigration like this (where test is evaluating a queryset, which is using custom manager with `.defer()` inside):

{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


def test_forwards(apps, schema_editor):
    ModelA = apps.get_model('blankapp', 'ModelA')
    list(ModelA.objects.all())


def noop(apps, schema_editor):
    return


class Migration(migrations.Migration):

    dependencies = [
        ('blankapp', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(test_forwards, noop)
    ]
}}}

test is failing like:

{{{
======================================================================
FAIL: test_defer_bug (blankapp.tests.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/projpath/blank/blankapp/tests.py"", line 8, in test_defer_bug
    self.assertIsInstance(ModelA.objects.last(), ModelA)
AssertionError: <ModelA_Deferred_test: ModelA_Deferred_test object> is not an instance of <class 'blankapp.models.ModelA'>
}}}


This is causing tests failures, when, for example, test contains FK assignment (there is `isinstance` check).

Test if failing from
https://github.com/django/django/commit/aa5ef0d4fc67a95ac2a5103810d0c87d8c547bac"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed		me@…	Ready for checkin	1	0	0	0	0	0
