<commit_before>import shortuuid

from django.db import models
from django.utils.encoding import force_unicode


class AutoUUIDField(models.SlugField):
    """Generates an automatic short UUID field."""
    description = "An automatic short UUID field."

    def __init__(self, *args, **kwargs):
        kwargs.setdefault('blank', True)
        kwargs.setdefault('editable', False)
        super(AutoUUIDField, self).__init__(*args, **kwargs)

    def _get_uuid(self):
        return shortuuid.uuid()

    def get_queryset(self, model_cls, slug_field):
        for field, model in model_cls._meta.get_fields_with_model():
            if model and field == slug_field:
                return model._default_manager.all()
        return model_cls._default_manager.all()

    def create_slug(self, model_instance, add):
        """Creates a unique shortuuid field."""
        slug = self._get_uuid()
        slug_field = model_instance._meta.get_field(self.attname)
        queryset = self.get_queryset(model_instance.__class__, slug_field)
        if model_instance.pk:
            queryset = queryset.exclude(pk=model_instance.pk)
        kwargs = {self.attname: slug}
        # Keep generating uuids until one found is free:
        while not slug or queryset.filter(**kwargs):
            slug = self._get_uuid()
            kwargs[self.attname] = slug
        return slug

    def pre_save(self, model_instance, add):
        value = force_unicode(self.create_slug(model_instance, add))
        setattr(model_instance, self.attname, value)
        return value

    def get_internal_type(self):
        return "SlugField"<commit_msg>Bugfix: Make sure the slug is not recreated on every save.<commit_after>import shortuuid

from django.db import models
from django.utils.encoding import force_unicode


class AutoUUIDField(models.SlugField):
    """Generates an automatic short UUID field."""
    description = "An automatic short UUID field."

    def __init__(self, *args, **kwargs):
        kwargs.setdefault('blank', True)
        kwargs.setdefault('editable', False)
        super(AutoUUIDField, self).__init__(*args, **kwargs)

    def _get_uuid(self):
        return shortuuid.uuid()

    def get_queryset(self, model_cls, slug_field):
        for field, model in model_cls._meta.get_fields_with_model():
            if model and field == slug_field:
                return model._default_manager.all()
        return model_cls._default_manager.all()

    def create_slug(self, model_instance, add):
        """Creates a unique shortuuid field."""
        slug = self._get_uuid()
        slug_field = model_instance._meta.get_field(self.attname)
        queryset = self.get_queryset(model_instance.__class__, slug_field)
        if model_instance.pk:
            queryset = queryset.exclude(pk=model_instance.pk)
        kwargs = {self.attname: slug}
        # Keep generating uuids until one found is free:
        while not slug or queryset.filter(**kwargs):
            slug = self._get_uuid()
            kwargs[self.attname] = slug
        return slug

    def pre_save(self, model_instance, add):
        # Get value from the current instance:
        value = getattr(model_instance, self.attname)
        if not value:
            value = force_unicode(self.create_slug(model_instance, add))
        setattr(model_instance, self.attname, value)
        return value

    def get_internal_type(self):
        return "SlugField"
{'fraction_non_alphanumeric': 0.09476744186046511, 'fraction_numerical': 0.0, 'mean_word_length': 2.896942242355606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7028590', 'n_tokens_mistral': 1010, 'n_tokens_neox': 977, 'n_words': 251}