|
@@ -2,10 +2,35 @@ from django.db import connection
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
+class CurrentTranslation(models.ForeignObject):
|
|
|
+ """
|
|
|
+ Creates virtual relation to the translation with model cache enabled.
|
|
|
+ """
|
|
|
+ # Avoid validation
|
|
|
+ requires_unique_target = False
|
|
|
+
|
|
|
+ def __init__(self, to, from_fields, to_fields, **kwargs):
|
|
|
+ # Disable reverse relation
|
|
|
+ kwargs['related_name'] = '+'
|
|
|
+ # Set unique to enable model cache.
|
|
|
+ kwargs['unique'] = True
|
|
|
+ super(CurrentTranslation, self).__init__(to, from_fields, to_fields, **kwargs)
|
|
|
+
|
|
|
+
|
|
|
+class ArticleTranslation(models.Model):
|
|
|
+
|
|
|
+ article = models.ForeignKey('indexes.Article')
|
|
|
+ language = models.CharField(max_length=10, unique=True)
|
|
|
+ content = models.TextField()
|
|
|
+
|
|
|
+
|
|
|
class Article(models.Model):
|
|
|
headline = models.CharField(max_length=100)
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
|
|
+ # Add virtual relation to the ArticleTranslation model.
|
|
|
+ translation = CurrentTranslation(ArticleTranslation, ['id'], ['article'])
|
|
|
+
|
|
|
class Meta:
|
|
|
index_together = [
|
|
|
["headline", "pub_date"],
|