Re: Nested fields error: Write an explicit update() method, but there is already one?

410 views
Skip to first unread message
Message has been deleted

Carl Nobile

unread,
Oct 31, 2023, 3:47:56 PM10/31/23
to django-res...@googlegroups.com
First of all, you need to get the formatting correct. It's not easy to read the way it is now, plus how would anyone know if the error you are getting is because the formatting on your end was a problem that caused your error?

The other thing which is even more important, you need to provide the Python error or traceback.

On Tue, Oct 31, 2023 at 11:09 AM Michael Shamoon <mic...@michaelshamoon.com> wrote:
Hiya, friend of mine and I are pretty stumped about a nested fields error, we're getting the following:
```AssertionError: The `.update()` method does not support writable nested fields by default. Write an explicit `.update()` method for serializer `documents.serialisers.DocumentSerializer`, or set `read_only=True` on nested serializer fields.``` but what is confusing is that there already is an update method. See the code below, really appreciate any thoughts.

```
class DocumentSerializer(OwnedObjectSerializer, DynamicFieldsModelSerializer):
...

custom_fields = CustomFieldInstanceSerializer(many=True, allow_null=True)

...

def update(self, instance, validated_data):
...
super().update(instance, validated_data)
return instance

def __init__(self, *args, **kwargs):
self.truncate_content = kwargs.pop("truncate_content", False)

super().__init__(*args, **kwargs)

class Meta:
model = Document
depth = 1
fields = (
"id",
"correspondent",
"document_type",
"storage_path",
"title",
"content",
"tags",
"created",
"created_date",
"modified",
"added",
"archive_serial_number",
"original_file_name",
"archived_file_name",
"owner",
"permissions",
"user_can_change",
"set_permissions",
"notes",
"custom_fields",
)
```

```
class CustomFieldSerializer(serializers.ModelSerializer):
class Meta:
model = CustomField
fields = [
"id",
"name",
"data_type",
]
```
```
class CustomFieldInstanceSerializer(serializers.ModelSerializer):
parent = CustomFieldSerializer()
value = SerializerMethodField()

def get_value(self, obj: CustomFieldInstance):
return obj.value

def create(self, validated_data):
parent_data = validated_data.pop("parent")
parent = CustomField.objects.get(id=parent_data["id"])
instance = CustomFieldInstance.objects.create(parent=parent)
return instance

def update(self, instance: CustomFieldInstance):
return instance

class Meta:
model = CustomFieldInstance
fields = [
"parent",
"value",
]
```

--
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/32ac3ee1-292d-4a82-9243-3790f1fb0538n%40googlegroups.com.


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