-
-
Notifications
You must be signed in to change notification settings - Fork 485
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
Unique fields in django model translation with Simple History #1299
Comments
This seems like an issue with a library you're using, or some code in your project, as Furthermore, the fields Does that seem plausible to you? |
Hey! In this context, it does make sense to maintain the 'unique' clause in the fields. If not, it may happen that my end user translates several different records with the same translation. About the fields you mentioned in my pull. They are classes inherited from django, CharField and TextField. |
So I'm also generally hesitant to add code for providing compatibility with just any library, as it has a tendency to make the code overall less maintainable, as maintainers have to spend time getting an idea of what each library does before being able to fully understand and then potentially fix/modify the code. Instead, I'm a much bigger fan of updating the documentation with suggestions users can implement themselves - like #579 did. Because if your code does indeed fix your issue, it seems to me that you can simply monkey-patch |
Migrations of translation fields from import copy
from django.apps import AppConfig, apps as django_apps
from modeltranslation.fields import TranslationField
from simple_history.models import HistoricalChanges, transform_field
class YourAppConfig(AppConfig):
name = "yourapp"
def ready(self):
# ...
# Register models for translation and history here
# ...
for model in django_apps.get_models():
if issubclass(model, HistoricalChanges):
for field in model._meta.fields:
if isinstance(field, TranslationField):
translated_field = copy.copy(field.translated_field)
transform_field(translated_field)
field.translated_field = translated_field There is no guarantee that nothing else will break with this solution :D. |
Closing stale issue. Please reopen if you'd like to discuss it further 🙂 |
Describe the bug
When creating models within django, there are fields that I need to be unique. These same fields can be translated and will also be unique.
However, when creating a migration, the translated fields in the historical table inherit the "Unique" characteristic.
I believe the bug is in Simple History because if I don't create the historical table, django works correctly.
But if I keep it active, I can create new records but I can't update them, it causes a sql error.
models.py:
translations.py
Excerpt from the migrations file
To Reproduce
Expected behavior
The migration was created without the 'unique' in the translatable fields without the need for manual editing.
Screenshots
If applicable, add screenshots to help explain your problem.
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: