code: def filter_models(request, models, exclude):
      
      items = get_avail_models(request)
      included = []
  
      def full_name(model):
          return '%s.%s' % (model.__module__, model.__name__)
  
      # I beleive that that implemented
      # O(len(patterns)*len(matched_patterns)*len(all_models))
      # algorythm is fine for model lists because they are small and admin
      # performance is not a bottleneck. If it is not the case then the code
      # should be optimized.
  
      if len(models) == 0:
          included = items
      else:
          for pattern in models:
              wildcard_models = []
              for item in items:
                  model, perms = item
                  model_str = full_name(model)
                  if model_str == pattern:
                      # exact match
                      included.append(item)
                  elif fnmatch(model_str, pattern) and \
                          item not in wildcard_models:
                      # wildcard match, put item in separate list so it can be
                      # sorted alphabetically later
                      wildcard_models.append(item)
              if wildcard_models:
                  # sort wildcard matches alphabetically before adding them
                  wildcard_models.sort(
                      key=lambda x: x[0]._meta.verbose_name_plural
                  )
                  included += wildcard_models
  
      result = included[:]
      for pattern in exclude:
          for item in included:
              model, perms = item
              if fnmatch(full_name(model), pattern):
                  try:
                      result.remove(item)
                  except ValueError:  # if the item was already removed skip
                      pass
      return result
docs: Returns (model, perm,) for all models that match models/exclude patterns
      and are visible by current user.