nodes: 0, module; 1, function_definition; 2, function_name:get_sorted_dependencies; 3, parameters; 4, block; 5, identifier:service_model; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, return_statement; 12, comment:"""
      Returns list of application models in topological order.
      It is used in order to correctly delete dependent resources.
      """; 13, assignment; 14, assignment; 15, assignment; 16, identifier:rel; 17, identifier:relations; 18, block; 19, call; 20, identifier:app_models; 21, call; 22, identifier:dependencies; 23, dictionary_comprehension; 24, identifier:relations; 25, generator_expression; 26, expression_statement; 27, identifier:stable_topological_sort; 28, argument_list; 29, identifier:list; 30, argument_list; 31, pair; 32, for_in_clause; 33, identifier:relation; 34, for_in_clause; 35, for_in_clause; 36, if_clause; 37, call; 38, identifier:app_models; 39, identifier:dependencies; 40, call; 41, identifier:model; 42, call; 43, identifier:model; 44, identifier:app_models; 45, identifier:model; 46, identifier:app_models; 47, identifier:relation; 48, attribute; 49, comparison_operator:relation.on_delete in (models.PROTECT, models.CASCADE); 50, attribute; 51, argument_list; 52, attribute; 53, argument_list; 54, identifier:set; 55, argument_list; 56, attribute; 57, identifier:related_objects; 58, attribute; 59, tuple; 60, subscript; 61, identifier:add; 62, attribute; 63, attribute; 64, identifier:get_models; 65, identifier:model; 66, identifier:_meta; 67, identifier:relation; 68, identifier:on_delete; 69, attribute; 70, attribute; 71, identifier:dependencies; 72, attribute; 73, identifier:rel; 74, identifier:related_model; 75, attribute; 76, identifier:app_config; 77, identifier:models; 78, identifier:PROTECT; 79, identifier:models; 80, identifier:CASCADE; 81, identifier:rel; 82, identifier:model; 83, identifier:service_model; 84, identifier:_meta
code: def get_sorted_dependencies(service_model):
      """
      Returns list of application models in topological order.
      It is used in order to correctly delete dependent resources.
      """
      app_models = list(service_model._meta.app_config.get_models())
      dependencies = {model: set() for model in app_models}
      relations = (
          relation
          for model in app_models
          for relation in model._meta.related_objects
          if relation.on_delete in (models.PROTECT, models.CASCADE)
      )
      for rel in relations:
          dependencies[rel.model].add(rel.related_model)
      return stable_topological_sort(app_models, dependencies)