-
Notifications
You must be signed in to change notification settings - Fork 127
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
Problem with GenericRelations on TranslationModel #285
Comments
Hello, |
My apologies, the original report was slightly wrong/misleading. We have a requirement to have an audit log for each translated model, so I added the following class (the class QATranslatedFields(TranslatedFields):
def _scan_model_bases(self, model):
bases, fields = super()._scan_model_bases(model)
bases = [QAMixin, AuditMixin] + bases
return bases, fields The abstract Obviously this isn't something you supported directly, but it did function perfectly. If you would like I can try and whip up a merge request to support this, but I'm not too sure where to start (other than perhaps filtering out any If this isn't a use case you want to support then that's fine, I can modify our code to not do it this way. |
Ok, I now understand how it gets there. So, this means, in your audit log, you have entries that actually point at a specific translation instead of the generic model. This is indeed something that's not supported, because the main concept of Now, it is true that when you use hvad in a project and you have a single instance where you want to interact with translations, it can make sense to “misuse” hvad a bit. Usually, when possible, I would recommend doing that by:
This does have the drawback of requiring the audit model to be aware of the language. I understand how this can be problematic in some cases. Anyway, I traced your issue, it comes from the optimizations that were added in the latest version of hvad:
This dramatically cuts down on the number of SQL requests: before, if you updated 5 translations in a DRF serializer, 10 SQL requests were sent (the untranslatable fields were updated 5 times). Now, only 6 requests are sent (1 for untranslatable fields and 1 per translation). As translation models cannot normally have m2m fields, the A proper fix will require building a sample testcase with failing test, which might actually be trickier than the fix itself. I cannot do it right now, but I should be able to spend some time on this in the weekend. |
Thanks for the great reply, I'll re-factor the AuditLog to have a language code and stop abusing hvad like this :) |
If you can do so, then definitely do. That will be much less trouble in the long run. I think this distinction of language as in “translating some field values” and language as in “semantically meaningful part of the database schema” should go in the FAQ, as it's commonly overlooked, but has strong implications. I'll add that next time I update it. Thanks for taking the time to report your issue :) |
Hey,
We are using
hvad
with DRF and it's amazing, thanks for the library and your time in making it.We've run into a small issue when updating to the new release. One of our models has a
GenericRelation
to anAuditLog
model:audit_logs = GenericRelation(AuditLog)
, and with the new release it triggers the following error when saving:As far as I can tell the problem lies within the
update_translation
method inrestframework/serializers.py
:This returns
audit_logs
in the list, but I think it should be excluded, as it's not an actual field on the model that can be updated.The text was updated successfully, but these errors were encountered: