          # Using 'pk' with only() should result in 3 deferred fields, namely all
          # of them except the model's primary key see #15494
          self.assert_delayed(qs.only("pk")[0], 3)
          # You can use 'pk' with reverse foreign key lookups.
          self.assert_delayed(self.s1.primary_set.all().only('pk')[0], 3)
  
      def test_defer_only_chaining(self):
          qs = Primary.objects.all()
          self.assert_delayed(qs.only("name", "value").defer("name")[0], 2)
          self.assert_delayed(qs.defer("name").only("value", "name")[0], 2)
 
</s> remove     def __set__(self, instance, value):
        """
        Deferred loading attributes can be set normally (which means there will
        never be a database lookup involved.
        """
        instance.__dict__[self.field_name] = value

 </s> add  </s> remove ``field_names`` are in the same order as the ``values``, so it is possible to
use ``cls(**(zip(field_names, values)))`` to instantiate the object. If all
of the model's fields are present, then ``values`` are guaranteed to be in
the order ``__init__()`` expects them. That is, the instance can be created
by ``cls(*values)``. It is possible to check if all fields are present by
consulting ``cls._deferred`` - if ``False``, then all fields have been loaded
from the database.
 </s> add ``field_names`` are in the same order as the ``values``. If all of the model's
fields are present, then ``values`` are guaranteed to be in the order
``__init__()`` expects them. That is, the instance can be created by
``cls(*values)``. If any fields are deferred, they won't appear in
``field_names``. In that case, assign a value of ``django.db.models.DEFERRED``
to each of the missing fields. </s> remove     def test_ticket_11936(self):
        app_config = apps.get_app_config("defer_regress")
        # Regression for #11936 - get_models should not return deferred models
        # by default. Run a couple of defer queries so that app registry must
        # contain some deferred classes. It might contain a lot more classes
        # depending on the order the tests are ran.
        list(Item.objects.defer("name"))
        list(Child.objects.defer("value"))
        klasses = {model.__name__ for model in app_config.get_models()}
        self.assertIn("Child", klasses)
        self.assertIn("Item", klasses)
        self.assertNotIn("Child_Deferred_value", klasses)
        self.assertNotIn("Item_Deferred_name", klasses)
        self.assertFalse(any(k._deferred for k in app_config.get_models()))

        klasses_with_deferred = {model.__name__ for model in app_config.get_models(include_deferred=True)}
        self.assertIn("Child", klasses_with_deferred)
        self.assertIn("Item", klasses_with_deferred)
        self.assertIn("Child_Deferred_value", klasses_with_deferred)
        self.assertIn("Item_Deferred_name", klasses_with_deferred)
        self.assertTrue(any(k._deferred for k in app_config.get_models(include_deferred=True)))

 </s> add  </s> remove         count = 0
        for field in obj._meta.fields:
            if isinstance(obj.__class__.__dict__.get(field.attname), DeferredAttribute):
                count += 1
 </s> add         count = len(obj.get_deferred_fields())
        # The related_id is alawys set even if it's not fetched from the DB,
        # so pk and related_id are not deferred.
        self.assert_delayed(self.s1.primary_set.all().only('pk')[0], 2)
          # Using 'pk' with only() should result in 3 deferred fields, namely all
          # of them except the model's primary key see #15494
          self.assert_delayed(qs.only("pk")[0], 3)
          # You can use 'pk' with reverse foreign key lookups.
        # The related_id is alawys set even if it's not fetched from the DB,
        # so pk and related_id are not deferred.
        self.assert_delayed(self.s1.primary_set.all().only('pk')[0], 2)
  
      def test_defer_only_chaining(self):
          qs = Primary.objects.all()
          self.assert_delayed(qs.only("name", "value").defer("name")[0], 2)
          self.assert_delayed(qs.defer("name").only("value", "name")[0], 2)
keep keep keep keep replace keep keep keep keep keep
 Fixed #26207 -- Replaced dynamic classes with non-data descriptors for deferred instance loading. 
tests/defer/tests.py