PrimaryKeyRelatedField with 'source' mapping to nullable foreign key field results in error while serializing #9218
Unanswered
osumoclement
asked this question in
Potential Issue
Replies: 0 comments
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.
Uh oh!
There was an error while loading. Please reload this page.
-
I am encountering a problem that's related to issue #5750
In my case, I am trying to use
PrimaryKeyRelatedField
which doesn't work.IntegerField
does work, but I really want to usePrimaryKeyRelatedField
for consistency with the rest of my code:#this doesn't work
foreign_field_attribute = serializers.PrimaryKeyRelatedField(source='foreign_field.my_attribute', allow_null=True, required=False)
#this works
foreign_field_attribute = serializers.IntegerField(source='foreign_field.my_attribute.id', allow_null=True, required=False)
As this is almost identical to issue #5750, I think the solution would be to apply the code that cleared issue up for
IntegerField
,CharField
etc toPrimaryKeyRelatedField
.UPDATE
The problem occurs when the object is created (i.e POST) and doesn't happen if it's a GET request. Even more strange is that at that point,
serializer.is_valid()
istrue
, but when you try to accessserializer.data
the error happensThe full debug log for this:
Beta Was this translation helpful? Give feedback.
All reactions