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
Can not update nested polymorphic object. I tried all methods (patch, put) but the drf exception occurs.
I checked all issues/solutions and nothing helps. I've stucked on it for 2 weeks.
Maybe I am doing wrong...
exception
The `.update()` method does not support writable nested fields by default.
Write an explicit `.update()` method for serializer `api.serializers.NodeSerializer`, or set `read_only=True` on nested serializer fields.
class SimpleContextSerializer(serializers.ModelSerializer):
class Meta:
model = SimpleContext
fields = ['id', 'field', 'node']
class TemplateContextSerializer(serializers.ModelSerializer):
class Meta:
model = TemplateContext
fields = ['id', 'field', 'node']
class ContextPolymorphicSerializer(PolymorphicSerializer):
model_serializer_mapping = {
SimpleContext: SimpleContextSerializer,
TemplateContext: TemplateContextSerializer
}
class NodeSerializer(UniqueFieldsMixin, NestedUpdateMixin, serializers.ModelSerializer):
context_set = ContextPolymorphicSerializer(many=True)
class Meta:
model = Node
fields = ['pk', 'context_set']
models.py
class Node(models.Model):
name = models.CharField(max_length=256)
user = models.ForeignKey(
get_user_model(),
on_delete=models.CASCADE,
null=True,
blank=True
)
parent = models.ForeignKey(
'Node',
on_delete=models.CASCADE,
null=True,
blank=True,
related_name='child_set'
)
created = models.DateTimeField(
null=True,
blank=True,
default=timezone.now
)
archived = models.DateTimeField(
null=True,
blank=True,
default=None
)
class Context(PolymorphicModel):
name = models.CharField(max_length=256)
service_set = models.ManyToManyField('Service')
field = models.ForeignKey(
'Field',
blank=True,
null=True,
on_delete=models.CASCADE
)
node = models.ForeignKey(
'Node',
blank=True,
null=True,
on_delete=models.SET_NULL
)
The text was updated successfully, but these errors were encountered:
kabancheg
changed the title
The .update() method does not support writable nested fields by default.
Django polymorphic + drf writable nested. The .update() method does not support writable nested fields by default.
Mar 30, 2021
Can not update nested polymorphic object. I tried all methods (patch, put) but the drf exception occurs.
I checked all issues/solutions and nothing helps. I've stucked on it for 2 weeks.
Maybe I am doing wrong...
exception
data
requirements
serializers.py
models.py
The text was updated successfully, but these errors were encountered: