How to name serializer field different than its class variable #8549
Unanswered
jacobg
asked this question in
Question & Answer
Replies: 1 comment
-
I'm not sure, but, are you trying to achieve something like this? 🤔 @jacobg
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Suppose I want to serialize an object that contains a field named 'in'.
The naive approach would be something like this:
However,
in
is a Python keyword, and will raise an error:SyntaxError: invalid syntax
Is there a simple solution to make MySerializer serialize that field using the name
'in
with a different class variable name?In DRF version 3.9.4, I made the following solution:
It worked great on that version, but upon updating to DRF version 3.13.1, it broke. I was able to fix it by changing the
TargetSerializerMixin
implementation to the following:Although it does work, it's fragile, as it relies on internal implementation details. As it already broke once, it could break again in a future DRF update. Is there a safer solution?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions