def METHOD_NAME(self, model_instance):
    """Generate a unique slug for a model instance."""
    # get fields to populate from and slug field to set
    slug_field = model_instance._meta.get_field(self.attname)
    slug = self.slugify(getattr(model_instance, self._populate_from))
    count = 0
    # strip slug depending on max_length attribute of the slug field
    # and clean-up
    slug_len = slug_field.max_length
    if slug_len:
        slug = slug[:slug_len]
    original_slug = slug
    # exclude the current model instance from the queryset used in finding
    # the next valid slug
    queryset = self.get_queryset(model_instance.__class__, slug_field)
    if model_instance.pk:
        queryset = queryset.exclude(pk=model_instance.pk)
    # form a kwarg dict used to implement any unique_together constraints
    kwargs = {}
    for params in model_instance._meta.unique_together:
        if self.attname in params:
            for param in params:
                kwargs[param] = getattr(model_instance, param, None)
    kwargs[self.attname] = slug
    # increases the number while searching for the next valid slug
    # depending on the given slug, clean-up
    while not slug or queryset.filter(**kwargs).exists():
        slug = original_slug
        end = self.uniquifying_suffix(count)
        end_len = len(end)
        if slug_len and len(slug) + end_len > slug_len:
            slug = slug[: slug_len - end_len]
        slug = slug + end
        kwargs[self.attname] = slug
        count += 1
    is_slug_valid = self.test_pattern.match(slug)
    if not is_slug_valid:
        # pylint: disable=broad-exception-raised
        raise Exception("Invalid generated slug: {slug}".format(slug=slug))
    return slug