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

Allow for defining translatable fields loosly on the model #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Bounder
Copy link

@Bounder Bounder commented Sep 12, 2019

Summary

I changed the TranslatableModelSerializer to allow for translatable fields to be defined directly on the serializer. This way, you can create flat models which return the translated values for a single language and allow for updating a model in a single language. The language for saving an object is derived from the LanguageCode field when present. Else it will use the default language detection of Django to determine the language for saving.

Reason / Use case

I was using django-hvad, which comes with a serializer which outputs the object in a flat structure as a single translation. I wanted to migrate my API from django-hvad to django-parler without changing the API structure.

Notes

I started out creating a separate FlatTranslatableModelSerialzer. When it worked I saw that the logic could be combined into the existing TranslatableModelSerializer without impacting the existing logic. I also didn't like the name of my new model serializer.
Currently, the serializer either pop's the translations field when finding it or it starts popping fields which are translatable. Another take to this would have been to enable/disable the flat structure with a param in init.

I also started out with creating a special LanguageCode field, but it seems that the auto-generated field by DRF had all functionality/validation needed. You can easily override the default LanguageCode field using the standard ChoiceField, i.e.: serializers.ChoiceField(choices=settings.LANGUAGES, required=False)

@Bounder
Copy link
Author

Bounder commented Oct 16, 2019

Just a friendly reminder to the maintainers that no reply was posted to this pull request. Is it good or bad?

@codecov
Copy link

codecov bot commented May 15, 2022

Codecov Report

Merging #30 (2c4a0f0) into master (81b6a13) will decrease coverage by 4.39%.
The diff coverage is 100.00%.

❗ Current head 2c4a0f0 differs from pull request most recent head dbc474a. Consider uploading reports for the commit dbc474a to get more accurate results

@@            Coverage Diff             @@
##           master      #30      +/-   ##
==========================================
- Coverage   89.62%   85.23%   -4.40%     
==========================================
  Files           4        4              
  Lines         135      149      +14     
==========================================
+ Hits          121      127       +6     
- Misses         14       22       +8     
Impacted Files Coverage Δ
parler_rest/serializers.py 100.00% <100.00%> (ø)
parler_rest/fields.py 74.11% <0.00%> (-9.80%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5c0a3e5...dbc474a. Read the comment docs.

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

Successfully merging this pull request may close these issues.

1 participant