djangorestframework-camel-case icon indicating copy to clipboard operation
djangorestframework-camel-case copied to clipboard

Setting to preserve _leadingUnderscores

Open rorybyrne opened this issue 5 years ago • 2 comments

Hi folks, in my API I have some keys with _leading_underscores, for example:

{
    '_str': 'String',
    '_timestamp': 1234567890,
    'firstValue': '...',
    'secondValue: '...'
}

I want to preserve these leading _'s, but camelize the other fields.

I am thinking about a PR that adds a setting PRESERVE_LEADING_UNDERSCORE, which would allow camelize_re in utils.py to be [a-z0-9]_[a-z0-9] instead of [a-z0-9]?_[a-z0-9] - note the changed '?'.

This would probably involve changing camelize_re from being a constant to being generated by a utils.py function, perhaps get_camelize_re().

Thoughts?

rorybyrne avatar Mar 27 '19 14:03 rorybyrne

I would like to add my vote +1 to this idea.

snowman2 avatar Mar 15 '21 13:03 snowman2

Until this is not added, a workaround is to override camelize_re regex. Adding the following code to the settings file works

import re
from djangorestframework_camel_case import util
util.camelize_re = re.compile(r"[a-z0-9]_[a-z0-9]")

pxotox avatar Jul 08 '21 17:07 pxotox