  
          qs = SpecialFeature.objects.only('feature__item__name').select_related('feature__item')
          self.assertEqual(len(qs), 1)
  
      def test_deferred_class_factory(self):
          new_class = deferred_class_factory(
              Item,
              ('this_is_some_very_long_attribute_name_so_modelname_truncation_is_triggered',))
          self.assertEqual(
              new_class.__name__,
              'Item_Deferred_this_is_some_very_long_attribute_nac34b1f495507dad6b02e2cb235c875e')
  
      def test_deferred_class_factory_already_deferred(self):
          deferred_item1 = deferred_class_factory(Item, ('name',))
          deferred_item2 = deferred_class_factory(deferred_item1, ('value',))
          self.assertIs(deferred_item2._meta.proxy_for_model, Item)
          self.assertNotIsInstance(deferred_item2.__dict__.get('name'), DeferredAttribute)
          self.assertIsInstance(deferred_item2.__dict__.get('value'), DeferredAttribute)
  
      def test_deferred_class_factory_no_attrs(self):
          deferred_cls = deferred_class_factory(Item, ())
          self.assertFalse(deferred_cls._deferred)
  
      @isolate_apps('defer_regress', kwarg_name='apps')
      def test_deferred_class_factory_apps_reuse(self, apps):
          """
          #25563 - model._meta.apps should be used for caching and
          retrieval of the created proxy class.
          """
          class BaseModel(models.Model):
              field = models.BooleanField()
  
              class Meta:
                  app_label = 'defer_regress'
  
          deferred_model = deferred_class_factory(BaseModel, ['field'])
          self.assertIs(deferred_model._meta.apps, apps)
          self.assertIs(deferred_class_factory(BaseModel, ['field']), deferred_model)
  
  
  class DeferAnnotateSelectRelatedTest(TestCase):
      def test_defer_annotate_select_related(self):
          location = Location.objects.create()
          Request.objects.create(location=location)
 
</s> remove # This function is needed because data descriptors must be defined on a class
# object, not an instance, to have any effect.

def deferred_class_factory(model, attrs):
    """
    Returns a class object that is a copy of "model" with the specified "attrs"
    being replaced with DeferredAttribute objects. The "pk_value" ties the
    deferred attributes to a particular instance of the model.
    """
    if not attrs:
        return model
    opts = model._meta
    # Never create deferred models based on deferred model
    if model._deferred:
        # Deferred models are proxies for the non-deferred model. We never
        # create chains of defers => proxy_for_model is the non-deferred
        # model.
        model = opts.proxy_for_model
    # The app registry wants a unique name for each model, otherwise the new
    # class won't be created (we get an exception). Therefore, we generate
    # the name using the passed in attrs. It's OK to reuse an existing class
    # object if the attrs are identical.
    name = "%s_Deferred_%s" % (model.__name__, '_'.join(sorted(attrs)))
    name = utils.truncate_name(name, 80, 32)

    try:
        return opts.apps.get_model(model._meta.app_label, name)

    except LookupError:

        class Meta:
            proxy = True
            apps = opts.apps
            app_label = opts.app_label

        overrides = {attr: DeferredAttribute(attr, model) for attr in attrs}
        overrides["Meta"] = Meta
        overrides["__module__"] = model.__module__
        overrides["_deferred"] = True
        return type(str(name), (model,), overrides)


 </s> add  </s> remove     cls = factory(model, attrs)
    return cls.__new__(cls)
 </s> add     return model.__new__(model) </s> remove     _deferred = False
 </s> add 
  
          qs = SpecialFeature.objects.only('feature__item__name').select_related('feature__item')
          self.assertEqual(len(qs), 1)
  



































  
  class DeferAnnotateSelectRelatedTest(TestCase):
      def test_defer_annotate_select_related(self):
          location = Location.objects.create()
          Request.objects.create(location=location)
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace keep keep keep keep keep
 Fixed #26207 -- Replaced dynamic classes with non-data descriptors for deferred instance loading. 
tests/defer_regress/tests.py