How to rename field?

23 views
Skip to first unread message

hec...@yandex.ru

unread,
Jul 25, 2020, 2:21:13 PM7/25/20
to Django REST framework
I have a SomeModelSerializer wich have foreign key. How to change name of field wich represented this foreign key from `some_object` to `some_object_id`?

Thanks!

Carl Nobile

unread,
Jul 26, 2020, 5:33:13 PM7/26/20
to django-res...@googlegroups.com
You can use the source argument in a serializer field.

inventory_type_public_id = serializers.CharField( 
    source='inventory_type.public_id', required=False)

In the above example `inventory_type_public_id` becomes the new field name, but it's source is `inventory_type.public_id`. Notice that the source name is a dotted path from the object.

~Carl

On Sat, Jul 25, 2020 at 2:21 PM hec...@yandex.ru <hec...@yandex.ru> wrote:
I have a SomeModelSerializer wich have foreign key. How to change name of field wich represented this foreign key from `some_object` to `some_object_id`?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/4468f426-3ee1-4f5d-9d51-67ebcb455f90n%40googlegroups.com.


--
-------------------------------------------------------------------------------
Carl J. Nobile (Software Engineer)
carl....@gmail.com
-------------------------------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages