MCPcopy
hub / github.com/django/django / traverse_qs

Method traverse_qs

tests/prefetch_related/tests.py:520–549  ·  view source on GitHub ↗

Helper method that returns a list containing a list of the objects in the obj_iter. Then for each object in the obj_iter, the path will be recursively travelled and the found objects are added to the return value.

(cls, obj_iter, path)

Source from the content-addressed store, hash-verified

518class CustomPrefetchTests(TestCase):
519 @classmethod
520 def traverse_qs(cls, obj_iter, path):
521 """
522 Helper method that returns a list containing a list of the objects in
523 the obj_iter. Then for each object in the obj_iter, the path will be
524 recursively travelled and the found objects are added to the return
525 value.
526 """
527 ret_val = []
528
529 if hasattr(obj_iter, "all"):
530 obj_iter = obj_iter.all()
531
532 try:
533 iter(obj_iter)
534 except TypeError:
535 obj_iter = [obj_iter]
536
537 for obj in obj_iter:
538 rel_objs = []
539 for part in path:
540 if not part:
541 continue
542 try:
543 related = getattr(obj, part[0])
544 except ObjectDoesNotExist:
545 continue
546 if related is not None:
547 rel_objs.extend(cls.traverse_qs(related, [part[1:]]))
548 ret_val.append((obj, rel_objs))
549 return ret_val
550
551 @classmethod
552 def setUpTestData(cls):

Calls 3

extendMethod · 0.80
allMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected