Skip to content

Commit

Permalink
Add an example of usage FunctionBasedFieldValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey-Skvortsov committed Feb 26, 2018
1 parent 91c093c commit dd358ed
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,55 @@ so the class Supplier could be wrote:
"type_tiers": RawFieldValue("S")
})
We also can refer by CompositeForeignKey in more flexible way using FunctionBasedFieldValue instead of RawFieldValue:

.. code:: python
from django.conf import global_settings
from django.utils.translation import get_language
class Supplier(models.Model):
company = models.IntegerField()
supplier_id = models.IntegerField()
class SupplierTranslations(TranslatedFieldsModel):
master = models.ForeignKey(Supplier, related_name='translations', null=True)
language_code = models.CharField(max_length=255, choices=global_settings.LANGUAGES)
name = models.CharField(max_length=255)
title = models.CharField(max_length=255)
class Meta:
unique_together = ('language_code', 'master')
active_translations = CompositeForeignKey(
SupplierTranslations,
on_delete=DO_NOTHING,
to_fields={
'master_id': 'id',
'language_code': FunctionBasedFieldValue(get_language)
})
active_translations.contribute_to_class(Supplier, 'active_translations')
in this example, the Supplier Model joins with SupplierTranslations in current active language and
supplier_instance.active_translations.name will return different names depend on
which language was activated by translation.activate(..):

.. code:: python
translation.activate('en')
print Supplier.objects.get(id=1).active_translations.name
translation.activate('your_language_code')
print Supplier.objects.get(id=1).active_translations.name
output should be:
'en_language_name'
'your_language_name'

Treate specific values as None
------------------------------
Expand Down

0 comments on commit dd358ed

Please sign in to comment.