Allow for defining translatable fields loosly on the model
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)
Just a friendly reminder to the maintainers that no reply was posted to this pull request. Is it good or bad?
Codecov Report
Merging #30 (2c4a0f0) into master (81b6a13) will decrease coverage by
4.39%. The diff coverage is100.00%.
:exclamation: 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%) |
:arrow_down: |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing dataPowered by Codecov. Last update 5c0a3e5...dbc474a. Read the comment docs.