Commits

Anonymous committed 4fd63f1

Added get_by_lang manager method

  • Participants
  • Parent commits acbc965

Comments (0)

Files changed (2)

 
 The ``current_language`` manager method is a deprecated alias for the last form.
 
+Retrieving a single translation object
+======================================
+
+The custom manager object has a shortcut for retrieving a single translation
+object, which may be very useful when used on related source objects. The method
+is named ``get_by_lang()`` and is called with an optional language code. The
+language code defaults to the currently active language. Here's an example::
+
+    my_source.translations.get_by_lang()  # Retrieves 'de' translation
+    my_source.translations.get_by_lang('es')  # Retrieves 'es' translation
+
+The added benefit of using this shortcut is that it reuses the existing
+queryset, so it works well with methods like ``prefetch_related``.
+
 Template tags
 =============
 

File i18n_model/models.py

     def current_language(self):
         return self.lang()
 
+    def get_by_lang(self, language_code=None):
+        language_code = language_code or get_language()
+        return self.get(i18n_language=language_code)
+
     def __new__(cls, *args, **kwargs):
         for language in settings.LANGUAGES:
             language_code = language[0]