  
  The ``db`` argument contains the database alias for the database the model
  is loaded from, ``field_names`` contains the names of all loaded fields, and
  ``values`` contains the loaded values for each field in ``field_names``. The
  ``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.
  
  In addition to creating the new model, the ``from_db()`` method must set the
  ``adding`` and ``db`` flags in the new instance's ``_state`` attribute.
  
  Below is an example showing how to record the initial values of fields that
 
</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         # default implementation of from_db() (could be replaced
        # with super())
        if cls._deferred:
            instance = cls(**zip(field_names, values))
        else:
            instance = cls(*values)
 </s> add         # Default implementation of from_db() (subject to change and could
        # be replaced with super()).
        if len(values) != len(cls._meta.concrete_fields):
            values = list(values)
            values.reverse()
            values = [
                values.pop() if f.attname in field_names else DEFERRED
                for f in cls._meta.concrete_fields
            ]
        new = cls(*values) </s> remove         self.assert_delayed(self.s1.primary_set.all().only('pk')[0], 3)
 </s> add         # 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)
``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.
  
  The ``db`` argument contains the database alias for the database the model
  is loaded from, ``field_names`` contains the names of all loaded fields, and
  ``values`` contains the loaded values for each field in ``field_names``. The
``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.
``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.
``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.
``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.
``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.
``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.
``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.
  
  In addition to creating the new model, the ``from_db()`` method must set the
  ``adding`` and ``db`` flags in the new instance's ``_state`` attribute.
  
  Below is an example showing how to record the initial values of fields that
keep keep keep keep 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. 
docs/ref/models/instances.txt