def get_next_unique_id(model, field, value):
    """
    Find next available incrementing value for a field in model.

    :param model: Model to be queried
    :param field: Model field to find value for
    :param value: Field value for which the next increment which is unique and available is to be found
    :return: the next unique increment value in model for the field considering index value from 1
    """

    condition = {}
    condition['%s__iregex' % field] = r'^%s[0-9]+$' % value
    values = model.objects.filter(**condition).values_list(field, flat=True)

    integers = map(lambda x: int(x.replace(value, '')), values)

    # complete sequence plus 1 extra if no gap exists
    all_values = range(1, len(integers) + 2)

    gap = list(set(all_values) - set(integers))[0]

    new_field_value = '%s%d' % (value, gap)

    return new_field_value
dataset/ETHPy150Open crowdresearch/crowdsource-platform/crowdsourcing/utils.py/get_next_unique_id/original