f***@gmail.com
2018-11-21 21:39:48 UTC
I have a model with an ArrayField containing CharField children. This in
turn is wired to the serializer through use of ModelSerializer. The field
is not specified explicitly in the serializer at all. However, when I'm
trying to update the model through PUT requests, I keep getting the
following error: {'keywords': {0: [ErrorDetail(string='Not a valid
string.', code='invalid')]}}.
The keywords argument is passed through a FormBody to the serializer as a
string that looks basically like "tag1,tag2,tag3", etc. Ideally this should
be split, saved to the model, and output as the serializer normally outputs
ListFields. However, it won't validate, and I haven't been able to figure
out exactly what I'm doing wrong.
I've attached the update method of the serializer in question. Please let
me know if any further information is needed and thank you in advance.
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
package = self.get_object()
default_version = package.version_set.get(
version_identifier=request.data['default_version']
)
package.default_version = default_version
# e.g. 'tag1,tag2,tag3'
keyword_arg = request.data['keywords'].lower()
# e.g. ['tag1', 'tag2', 'tag3']
keywords = split_keywords(keywords)
updated_data = request.data.copy()
updated_data['default_version'] = package.default_version.id
updated_data['keywords'] = keywords
serializer = self.get_serializer(package, data=updated_data,
partial=partial)
try:
serializer.is_valid(raise_exception=True)
except Exception as err:
print(err)
raise err
self.perform_update(serializer)
return response.Response(serializer.data)
turn is wired to the serializer through use of ModelSerializer. The field
is not specified explicitly in the serializer at all. However, when I'm
trying to update the model through PUT requests, I keep getting the
following error: {'keywords': {0: [ErrorDetail(string='Not a valid
string.', code='invalid')]}}.
The keywords argument is passed through a FormBody to the serializer as a
string that looks basically like "tag1,tag2,tag3", etc. Ideally this should
be split, saved to the model, and output as the serializer normally outputs
ListFields. However, it won't validate, and I haven't been able to figure
out exactly what I'm doing wrong.
I've attached the update method of the serializer in question. Please let
me know if any further information is needed and thank you in advance.
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
package = self.get_object()
default_version = package.version_set.get(
version_identifier=request.data['default_version']
)
package.default_version = default_version
# e.g. 'tag1,tag2,tag3'
keyword_arg = request.data['keywords'].lower()
# e.g. ['tag1', 'tag2', 'tag3']
keywords = split_keywords(keywords)
updated_data = request.data.copy()
updated_data['default_version'] = package.default_version.id
updated_data['keywords'] = keywords
serializer = self.get_serializer(package, data=updated_data,
partial=partial)
try:
serializer.is_valid(raise_exception=True)
except Exception as err:
print(err)
raise err
self.perform_update(serializer)
return response.Response(serializer.data)
--
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.