KristianOellegaard/django-hvad — pull request #138 by spectras (2013-11-13 09:16:48+00:00)
Title: Custom TranslationAware- -Manager and -Queryset

I am missing the ability to use custom manager and querysets which are a great feature of Django to split the logic and the implementation (Article.objects.published()). A good step was taken in adding the queryset_class setting to TranslationManager, but TranslationAwareManager is still lacking this ability.

This is an attempt at creating a cleverer get_translation_aware_manager, and a utility function on the TranslationManager that returns a translation-aware queryset.

The goal is to enable this:
```python
class MyModelFiltersMixin(object):
def my_filter(self, something):
return self.filter(my_translated_field__contains=something)
class MyModelQueryset(TranslationQueryset, MyModelFiltersMixin):
pass
class MyModelManager(TranslationManager, MyModelFiltersMixin):
queryset_class = MyModelQueryset

MyModel.objects.my_filter('foo') # this won't work as fallback is not translation aware
MyModel.objects.language('en').my_filter('foo') # this already works with current codebase
MyModel.objects.any_language().my_filter('foo') # this is new, it works as any_language makes is aware
get_translation_aware_manager(MyModel).my_filter('foo') # this is new too
get_translation_aware_manager(MyModel).all().my_filter('foo') # as is this (note the all() forcing a queryset)
```

It does have a caveat though: custom methods cannot use super() or they will break when invoked.
AFAIK it should not break existing code as:
* any_language() did not exist at all. Could break existing code defining a custom manager having an any_language function though.
* get_translation_aware_manager used to return no custom method at all, so existing code won't run into the no-super caveat while trying to call one.
* there could be an issue with custom manager explicitly inheriting both Manager and TranslationManager. That is an odd thing to do, but a sensible exception message will be raised.
I added some testing.

What do you think about this approach?