#25563: Datamigration causing incorrect behavior when custom manager is using
.defer()
----------------------------+--------------------
     Reporter:  coldmind    |      Owner:  nobody
         Type:  Bug         |     Status:  new
    Component:  Migrations  |    Version:  1.8
     Severity:  Normal      |   Keywords:
 Triage Stage:  Unreviewed  |  Has patch:  0
Easy pickings:  0           |      UI/UX:  0
----------------------------+--------------------
 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

--
Ticket URL: <https://code.djangoproject.com/ticket/25563>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.ea6761ec3c5d0f9b91d2a8ad23d32764%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to