def get_next_unique_id(model, field, value):
 condition = {}
 condition[('%s__iregex' % field)] = ('^%s[0-9]+$' % value)
 values = model.objects.filter(**condition).values_list(field, flat=True)
 integers = map((lambda x: int(x.replace(value, ''))), values)
 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

def get_next_unique_id ( model , field , value ) : condition = { } condition [ ( '%s__iregex' % field ) ] = ( '^%s[0-9]+$' % value ) values = model . objects . filter ( ** condition ) . values_list ( field , flat = True ) integers = map ( ( lambda x : int ( x . replace ( value , '' ) ) ) , values ) 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
find next available incrementing value for a field in model .