CamelCase field names

762 views
Skip to first unread message

jus...@battlefy.com

unread,
Jan 15, 2013, 6:59:44 PM1/15/13
to django-res...@googlegroups.com
What would be a good way to achieve CamelCase field names with django-rest-framework?

Our Django models' fields are defined with with underscore.. for example, "start_date = models.DateTimeField()"

In Javascript, camelCase is most widely adopted. So, I would want to access the variable above as "startDate".

Any suggestions? Many thanks.

Cheers,
Justin

Matteo Suppo

unread,
Jan 16, 2013, 7:51:15 AM1/16/13
to django-res...@googlegroups.com
You can do something like 

startDate = models.DateTimeField(source='start_date')

but you will have to do it for every field.

jus...@battlefy.com

unread,
Jan 16, 2013, 1:04:20 PM1/16/13
to django-res...@googlegroups.com
Yes. I can definitely do that.

I was wondering if there's a easier way to intercept the creation of those field and make them CamelCase.

Thanks,
Justin

Tom Christie

unread,
Jan 17, 2013, 3:48:09 AM1/17/13
to django-res...@googlegroups.com
I was wondering if there's a easier way to intercept the creation of those field and make them CamelCase.

Not at the moment, no.
I would like that to be supported tho'.
Something along the lines of a 'get_field_name' on the serializer class that can be overridden, and a setting that defines the default function and allows secs to switch to a different default style.
If you think that's something you'd be interested in implementing then fell free to open a ticket and we can talk through the implementation there.

Cheers,

  Tom

Steven Kane

unread,
Jan 23, 2013, 4:03:18 PM1/23/13
to django-res...@googlegroups.com
Tom and all,

I am also implementing a similar conversion step except in my particular case I'm looking to convert PrimaryKeyRelatedField's into the following format:

#on model
parent = models.ForeignKey()

#on serializer
parent_id = serializer.PrimaryKeyRelatedField(source='parent')

is that correct for doing this process on each field manually?

Steve

Steven Kane

unread,
Jan 23, 2013, 4:38:33 PM1/23/13
to django-res...@googlegroups.com
Ok I jumped the gun a bit as it's early morning

I have implemented these hardcoded changes into my model and it works as expected though I am only interested in changing the behavior of related fields.

#on serializer
class MySerializer (serializers.ModelSerializer):
  class Meta:
     model = MyModel
     exclude = ('my_second_model',)
  my_second_model_id = serializers.PrimaryKeyRelatedField(source='my_second_model')

Steve

Jacob G

unread,
Jun 21, 2013, 1:32:45 PM6/21/13
to django-res...@googlegroups.com
Hi, Was just wondering if this was ever implemented? Doesn't seem so. I'm also interesting in doing this.

Vitaly Babiy

unread,
Jun 22, 2013, 2:45:17 PM6/22/13
to django-res...@googlegroups.com
Here is the solution I have been using:

Jacob G

unread,
Jun 23, 2013, 12:31:12 AM6/23/13
to django-res...@googlegroups.com
Thanks Vitaly. That works nicely for rendering, but I don't think it does the inverse, i.e., to write/deserialize data.

Jacob G

unread,
Jun 23, 2013, 9:00:17 AM6/23/13
to django-res...@googlegroups.com
The following Git issue has been created: https://github.com/tomchristie/django-rest-framework/issues/944

Vitaly Babiy

unread,
Jul 4, 2013, 4:54:58 PM7/4/13
to django-res...@googlegroups.com
I have update the gist with a parser.

Reply all
Reply to author
Forward
0 new messages