Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using TranslatedFieldsField for translations field in update , partial update model view set #41

Open
farouk46015 opened this issue Mar 16, 2023 · 0 comments

Comments

@farouk46015
Copy link

I have model translation include
fox ex : title , slug fields

the problem on update you get error when serializing data that field slug all ready exist

the cause of the problem in TranslatedFieldsField class in to_internal_value fun

serializer = self.serializer_class(data=model_fields)

so in update we have to pass instance to the serializer

I worked on it

` def to_internal_value(self, data):
"""
Deserialize data from translations fields.

    For each received language, delegate validation logic to
    the translation model serializer.
    """
    if data is None:
        return

    if not isinstance(data, dict):
        self.fail('invalid')
    if not self.allow_empty and len(data) == 0:
        self.fail('empty')
    if self.shared_model is None:
        return

    # related_name = self.source or field_name
    result, errors, = {}, {}
    translated_model = self.shared_model.__dict__[self.source].field.model
    for lang_code, model_fields in data.items():
        if 'id' in model_fields:
            instance = translated_model.objects.get(pk=model_fields['id'])
            serializer = self.serializer_class(instance, data=model_fields, partial=self.parent.partial)
        else:
            serializer = self.serializer_class(data=model_fields)
        if serializer.is_valid():
            result[lang_code] = serializer.validated_data
        else:
            errors[lang_code] = serializer.errors

    if errors:
        raise serializers.ValidationError(errors)
    return result`

so could you edit your code so it would work on update and partial update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant