Rename variables in polymorphic/query.py for clarity
(split from previous commit by @jedediah)fix_request_path_info
parent
19d5ed2338
commit
ea9cb91e78
|
|
@ -144,8 +144,8 @@ class PolymorphicQuerySet(QuerySet):
|
||||||
# - also record the correct result order in "ordered_id_list"
|
# - also record the correct result order in "ordered_id_list"
|
||||||
# - store objects that already have the correct class into "results"
|
# - store objects that already have the correct class into "results"
|
||||||
base_result_objects_by_id = {}
|
base_result_objects_by_id = {}
|
||||||
self_model_content_type_id = ContentType.objects.get_for_model(self.model, for_concrete_model=False).pk
|
self_model_class_id = ContentType.objects.get_for_model(self.model, for_concrete_model=False).pk
|
||||||
self_concrete_model_content_type_id = ContentType.objects.get_for_model(self.model, for_concrete_model=True).pk
|
self_concrete_model_class_id = ContentType.objects.get_for_model(self.model, for_concrete_model=True).pk
|
||||||
|
|
||||||
for base_object in base_result_objects:
|
for base_object in base_result_objects:
|
||||||
ordered_id_list.append(base_object.pk)
|
ordered_id_list.append(base_object.pk)
|
||||||
|
|
@ -157,21 +157,21 @@ class PolymorphicQuerySet(QuerySet):
|
||||||
|
|
||||||
base_result_objects_by_id[base_object.pk] = base_object
|
base_result_objects_by_id[base_object.pk] = base_object
|
||||||
|
|
||||||
if (base_object.polymorphic_ctype_id == self_model_content_type_id):
|
if (base_object.polymorphic_ctype_id == self_model_class_id):
|
||||||
# Real class is exactly the same as base class, go straight to results
|
# Real class is exactly the same as base class, go straight to results
|
||||||
results[base_object.pk] = base_object
|
results[base_object.pk] = base_object
|
||||||
|
|
||||||
else:
|
else:
|
||||||
modelclass = base_object.get_real_instance_class()
|
real_concrete_class = base_object.get_real_instance_class()
|
||||||
real_concrete_class_id = base_object.get_real_concrete_instance_class_id()
|
real_concrete_class_id = base_object.get_real_concrete_instance_class_id()
|
||||||
|
|
||||||
if real_concrete_class_id == self_concrete_model_content_type_id:
|
if real_concrete_class_id == self_concrete_model_class_id:
|
||||||
# Real and base classes share the same concrete ancestor,
|
# Real and base classes share the same concrete ancestor,
|
||||||
# upcast it and put it in the results
|
# upcast it and put it in the results
|
||||||
results[base_object.pk] = transmogrify(modelclass, base_object)
|
results[base_object.pk] = transmogrify(real_concrete_class, base_object)
|
||||||
else:
|
else:
|
||||||
modelclass = ContentType.objects.get_for_id(real_concrete_class_id).model_class()
|
real_concrete_class = ContentType.objects.get_for_id(real_concrete_class_id).model_class()
|
||||||
idlist_per_model[modelclass].append(base_object.pk)
|
idlist_per_model[real_concrete_class].append(base_object.pk)
|
||||||
|
|
||||||
# django's automatic ".pk" field does not always work correctly for
|
# django's automatic ".pk" field does not always work correctly for
|
||||||
# custom fields in derived objects (unclear yet who to put the blame on).
|
# custom fields in derived objects (unclear yet who to put the blame on).
|
||||||
|
|
@ -187,29 +187,29 @@ class PolymorphicQuerySet(QuerySet):
|
||||||
# Then we copy the annotate fields from the base objects to the real objects.
|
# Then we copy the annotate fields from the base objects to the real objects.
|
||||||
# Then we copy the extra() select fields from the base objects to the real objects.
|
# Then we copy the extra() select fields from the base objects to the real objects.
|
||||||
# TODO: defer(), only(): support for these would be around here
|
# TODO: defer(), only(): support for these would be around here
|
||||||
for modelclass, idlist in idlist_per_model.items():
|
for real_concrete_class, idlist in idlist_per_model.items():
|
||||||
qs = modelclass.base_objects.filter(pk__in=idlist) # use pk__in instead ####
|
real_objects = real_concrete_class.base_objects.filter(pk__in=idlist) # use pk__in instead ####
|
||||||
qs.dup_select_related(self) # copy select related configuration to new qs
|
real_objects.dup_select_related(self) # copy select related configuration to new qs
|
||||||
|
|
||||||
for o in qs:
|
for real_object in real_objects:
|
||||||
o_pk = getattr(o, pk_name)
|
o_pk = getattr(real_object, pk_name)
|
||||||
real_class = o.get_real_instance_class()
|
real_class = real_object.get_real_instance_class()
|
||||||
|
|
||||||
# If the real class is a proxy, upcast it
|
# If the real class is a proxy, upcast it
|
||||||
if real_class != modelclass:
|
if real_class != real_concrete_class:
|
||||||
o = transmogrify(real_class, o)
|
real_object = transmogrify(real_class, real_object)
|
||||||
|
|
||||||
if self.query.aggregates:
|
if self.query.aggregates:
|
||||||
for anno_field_name in self.query.aggregates.keys():
|
for anno_field_name in self.query.aggregates.keys():
|
||||||
attr = getattr(base_result_objects_by_id[o_pk], anno_field_name)
|
attr = getattr(base_result_objects_by_id[o_pk], anno_field_name)
|
||||||
setattr(o, anno_field_name, attr)
|
setattr(real_object, anno_field_name, attr)
|
||||||
|
|
||||||
if self.query.extra_select:
|
if self.query.extra_select:
|
||||||
for select_field_name in self.query.extra_select.keys():
|
for select_field_name in self.query.extra_select.keys():
|
||||||
attr = getattr(base_result_objects_by_id[o_pk], select_field_name)
|
attr = getattr(base_result_objects_by_id[o_pk], select_field_name)
|
||||||
setattr(o, select_field_name, attr)
|
setattr(real_object, select_field_name, attr)
|
||||||
|
|
||||||
results[o_pk] = o
|
results[o_pk] = real_object
|
||||||
|
|
||||||
# re-create correct order and return result list
|
# re-create correct order and return result list
|
||||||
resultlist = [results[ordered_id] for ordered_id in ordered_id_list if ordered_id in results]
|
resultlist = [results[ordered_id] for ordered_id in ordered_id_list if ordered_id in results]
|
||||||
|
|
@ -217,14 +217,14 @@ class PolymorphicQuerySet(QuerySet):
|
||||||
# set polymorphic_annotate_names in all objects (currently just used for debugging/printing)
|
# set polymorphic_annotate_names in all objects (currently just used for debugging/printing)
|
||||||
if self.query.aggregates:
|
if self.query.aggregates:
|
||||||
annotate_names = self.query.aggregates.keys() # get annotate field list
|
annotate_names = self.query.aggregates.keys() # get annotate field list
|
||||||
for o in resultlist:
|
for real_object in resultlist:
|
||||||
o.polymorphic_annotate_names = annotate_names
|
real_object.polymorphic_annotate_names = annotate_names
|
||||||
|
|
||||||
# set polymorphic_extra_select_names in all objects (currently just used for debugging/printing)
|
# set polymorphic_extra_select_names in all objects (currently just used for debugging/printing)
|
||||||
if self.query.extra_select:
|
if self.query.extra_select:
|
||||||
extra_select_names = self.query.extra_select.keys() # get extra select field list
|
extra_select_names = self.query.extra_select.keys() # get extra select field list
|
||||||
for o in resultlist:
|
for real_object in resultlist:
|
||||||
o.polymorphic_extra_select_names = extra_select_names
|
real_object.polymorphic_extra_select_names = extra_select_names
|
||||||
|
|
||||||
return resultlist
|
return resultlist
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue