Purpose of checking current instance in every iteration of while loop?
If I understand correctly, these 2 lines make sure that the current instance is not already in the database.
if instance.pk: rivals = rivals.exclude(pk=instance.pk)
Why don't we move this check before the
while loop and return if
instance.pk is not
None (a valid
instance.pk implies it's not a new object but an update operation) and hence there's no need to re-generate a unique slug. The
while loop can be skipped in that case.