You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You cannot call .save() after accessing serializer.data.If you need to access data before committing to the database then inspect 'serializer.validated_data' instead.
#149
Open
iyinolu opened this issue
Sep 3, 2021
· 2 comments
class UserSerializer(UniqueFieldsMixin, serializers.ModelSerializer):
class Meta:
model = User
fields = [
"pk",'firstName', 'lastName', 'email', 'gender',
'phoneNumber', 'dateOfBirth', 'userType',
'dateCreated', 'lastModified'
]
class PartnerUserSerializer(
NestedCreateMixin,
NestedUpdateMixin,
serializers.ModelSerializer ):
primaryInfo = UserSerializer()
class Meta:
model = PartnerUser
fields = [
"pk", "primaryInfo", "address", "maritalStatus",
"nationRegNumber", "bankVerifiNum",
"affiliates"
]
Here is the view class:
class PartnerUserUpdateView(UpdateAPIView):
queryset = PartnerUser.objects.all()
serializer_class = PartnerUserSerializer
primaryInfo field is serialized using UserSerializer. but unique validation doesnt work and i get the error mentioned in this issue.
However, overriding the following assert condition on the save method in BaseSerializer class offered a bypass:
assert not hasattr(self, '_data'), (
"You cannot call `.save()` after accessing `serializer.data`."
"If you need to access data before committing to the database then "
"inspect 'serializer.validated_data' instead. "
)
I get this error when i try to update a model instance with nested fields.
The text was updated successfully, but these errors were encountered: