<commit_before>import logging
logger = logging.getLogger(__name__)

from django.db import models


def get_next_ordering(model, field_name='sort_order', increment=10):
    """
    Get the next available value for the sortorder for a model.

    Use case::

        class MyModel(models.Model):
            sort_order = models.PositiveSmallIntegerField(
                default=lambda: get_next_ordering(MyModel)
            )

    """
    aggregate = model.objects.aggregate(latest=models.Max(field_name))
    latest = aggregate['latest']
<commit_msg>Allow QuerySets in get_next_ordering<commit_after>import logging
logger = logging.getLogger(__name__)

from django.db import models


def get_next_ordering(model_or_qs, field_name='sort_order', increment=10):
    """
    Get the next available value for the sortorder for a model.

    Use case::

        class MyModel(models.Model):
            sort_order = models.PositiveSmallIntegerField(
                default=lambda: get_next_ordering(MyModel)
            )

    """
    if isinstance(model_or_qs, models.base.ModelBase):
        # If a model has been given, create QuerySet for all objects
        qs = model_or_qs.objects.all()
    else:
        # No model has been given, assert that the input is in fact
        # a QuerySet.
        assert isinstance(model_or_qs, models.query.QuerySet)
        qs = model_or_qs

    aggregate = qs.aggregate(latest=models.Max(field_name))
    latest = aggregate['latest']

{'fraction_non_alphanumeric': 0.08633582596872875, 'fraction_numerical': 0.002719238613188307, 'mean_word_length': 3.043956043956044, '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': '6473324', 'n_tokens_mistral': 434, 'n_tokens_neox': 411, 'n_words': 109}