hvad.utils

hvad.utils.get_cached_translation(instance)

Returns the cached translation from an instance or None. Encapsulates a getattr() using the model’s translations_cache.

hvad.utils.set_cached_translation(instance, translation)

Sets the currently cached translation for the instance, and returns the translation that was loaded before the call. Passing None as translation will unload current translation and let the instance untranslated.

hvad.utils.combine(trans, klass)

Combines a Shared Model with a Translations Model by taking the Translations Model and setting it onto the Shared Model‘s translations cache.

klass is the Shared Model class. This argument is required as there is no way to distinguish a translation of a proxy model from that of a concrete model otherwise.

This function is only intended for loading models from the database. For other uses, set_cached_translation() should be used instead.

hvad.utils.get_translation(instance, language_code=None)

Returns the translation for an instance, in the specified language. If given language is None, uses get_language() to get current language.

Encapsulates a getattr() using the model’s translations_accessor and a call to its get() method using the instance’s primary key and given language_code as filters.

hvad.utils.load_translation(instance, language, enforce=False)

Returns the translation for an instance.

  • If enforce is False, then language is used as a default language, if the instance has no language currently loaded.
  • If enforce is True, then language will be enforced upon the translation, ignoring cached translation if it is not in the given language.

A valid translation instance is always returned. It will be loaded from the database as required. If this fails, a new, empty, ready-to-use translation will be returned.

The instance itself is untouched.

hvad.utils.get_translation_aware_manager(model)

Returns a manager for a normal model that is aware of translations and can filter over translated fields on translated models related to this normal model.

class hvad.utils.SmartGetFieldByName

Smart version of the standard get_field_by_name() on the options (meta) of Django models that raises a more useful exception when one tries to access translated fields with the wrong manager.

This descriptor is pending deprecation as the associated method is being removed from Django.

__init__(self, real)

Retains a reference to the actual method this descriptor is replacing.

__call__(self, meta, name)

Catches improper use of the get_field_by_name method to access translated fields and raise a WrongManager exception.

class hvad.utils.SmartGetField

Smart version of the standard get_field() on the options (meta) of Django models that raises a more useful exception when one tries to access translated fields with the wrong manager.

__init__(self, real)

Retains a reference to the actual method this descriptor is replacing.

__call__(self, meta, name)

Catches improper use of the get_field method to access translated fields and raise a WrongManager exception.

class hvad.utils._MinimumDjangoVersionDescriptor

Helper class used by minimumDjangoVersion() decorator.

hvad.utils.minimumDjangoVersion(*args)

Decorator that will catch attempts to use methods on a Django version that does not support them and raise a helpful exception.

Arguments must be the minimum allowable Django version, the will be compared against the django.VERSION tuple.

settings_updater(func):

Decorator for setting globals depending on Django settings. It simply invokes the decorated function immediately, then calls it again every time the setting_changed signal is sent by Django.