docs: def populate_slug(instance, field):
      '''
      Populate a slug field if needed.
      '''
      value = getattr(instance, field.db_field)
  
      try:
          previous = instance.__class__.objects.get(id=instance.id)
      except Exception:
          previous = None
  
      # Field value has changed
      changed = field.db_field in instance._get_changed_fields()
      # Field initial value has been manually set
      manual = not previous and value or changed
  
      if not manual and field.populate_from:
          # value to slugify is extracted from populate_from parameter
          value = getattr(instance, field.populate_from)
          if previous and value == getattr(previous, field.populate_from):
              return value
  
      if previous and getattr(previous, field.db_field) == value:
          # value is unchanged from DB
          return value
  
      if previous and not changed and not field.update:
          # Field is not manually set and slug should not update on change
          return value
  
      slug = field.slugify(value)
  
      # This can happen when serializing an object which does not contain
      # the properties used to generate the slug. Typically, when such
      # an object is passed to one of the Celery workers (see issue #20).
      if slug is None:
          return
  
      old_slug = getattr(previous, field.db_field, None)
  
      if slug == old_slug:
          return slug
  
      # Ensure uniqueness
      if field.unique:
          base_slug = slug
          index = 1
          qs = instance.__class__.objects
          if previous:
              qs = qs(id__ne=previous.id)
  
          def exists(s):
              return qs(
                  class_check=False, **{field.db_field: s}
              ).limit(1).count(True) > 0
  
          while exists(slug):
              slug = '{0}-{1}'.format(base_slug, index)
              index += 1
  
      # Track old slugs for this class
      if field.follow and old_slug != slug:
          ns = instance.__class__.__name__
          # Destroy redirections from this new slug
          SlugFollow.objects(namespace=ns, old_slug=slug).delete()
  
          if old_slug:
              # Create a redirect for previous slug
              slug_follower, created = SlugFollow.objects.get_or_create(
                  namespace=ns,
                  old_slug=old_slug,
                  auto_save=False,
              )
              slug_follower.new_slug = slug
              slug_follower.save()
  
              # Maintain previous redirects
              SlugFollow.objects(namespace=ns, new_slug=old_slug).update(new_slug=slug)
  
      setattr(instance, field.db_field, slug)
      return slug