'Converts the self.deferred_loading data structure to an alternate data
structure, describing the field that *will* be loaded. This is used to
compute the columns to select from the database and also by the
QuerySet class to work out which fields are being initialised on each
model. Models that have all their fields included aren\'t mentioned in
the result, only those that have field restrictions in place.
The "target" parameter is the instance that is populated (in place).
The "callback" is a function that is called whenever a (model, field)
pair need to be added to "target". It accepts three parameters:
"target", and the model and list of fields being added for that model.'

def deferred_to_data(self, target, callback):

    (field_names, defer) = self.deferred_loading
    if (not field_names):
        return
    columns = set()
    orig_opts = self.model._meta
    seen = {}
    must_include = {self.model: set([orig_opts.pk])}
    for field_name in field_names:
        parts = field_name.split(LOOKUP_SEP)
        cur_model = self.model
        opts = orig_opts
        for name in parts[:(-1)]:
            old_model = cur_model
            source = opts.get_field_by_name(name)[0]
            cur_model = opts.get_field_by_name(name)[0].rel.to
            opts = cur_model._meta
            must_include[old_model].add(source)
            add_to_dict(must_include, cur_model, opts.pk)
        (field, model, _, _) = opts.get_field_by_name(parts[(-1)])
        if (model is None):
            model = cur_model
        add_to_dict(seen, model, field)
    if defer:
        workset = {}
        for (model, values) in seen.iteritems():
            for (field, m) in model._meta.get_fields_with_model():
                if (field in values):
                    continue
                add_to_dict(workset, (m or model), field)
        for (model, values) in must_include.iteritems():
            if (model in workset):
                workset[model].update(values)
        for (model, values) in workset.iteritems():
            callback(target, model, values)
    else:
        for (model, values) in must_include.iteritems():
            if (model in seen):
                seen[model].update(values)
            else:
                seen[model] = values
        for model in orig_opts.get_parent_list():
            if (model not in seen):
                seen[model] = set()
        for (model, values) in seen.iteritems():
            callback(target, model, values)
