Reputation: 37876
I am building my own custom manager to get translations of a model. The Idea is:
main model:
class BlogTranslationManager(models.Manager):
def language(self):
lang = translation.get_language()
return BlogTranslation.objects.filter(lang=lang)
class ArticleTranslationManager(models.Manager):
def language(self):
lang = translation.get_language()
return ArticleTranslation.objects.filter(lang=lang)
class Blog(models.Model):
created = models.DateTimeField()
objects = models.Manager()
translations = BlogTranslationManager()
# PROBLEM HERE
@property
def articles(self):
# here I need translations of all articles of this blog
# I cannot do:
return self.blog_articles.translations.language('en')
# because 'translations' Manager cannot be called on QuerySet
# what can I do here? am I missing something in CustomManagers?
class Article(models.Model):
blog = models.ForeignKey(Blog, related_name="blog_articles")
objects = models.Manager()
translations = ArticleTranslationManager()
class BlogTranslation(models.Model):
book = models.ForeignKey(Book, related_name="book_translations")
field_name = models.CharField(max_length=10)
lang = models.CharField(max_length=3)
translation = models.TextField()
class ArticleTranslation(models.Model):
article = models.ForeignKey(Article, related_name="article_translations")
field_name = models.CharField(max_length=10)
lang = models.CharField(max_length=3)
translation = models.TextField()
and in template, I want to able to call:
{{ blog.articles }}
I described the problem right inside the code above, any ideas about it? any help is highly appreciated!
Upvotes: 0
Views: 217
Reputation: 308839
Try using the Article.translations
manager first, then filter the resulting queryset. If I understand your models correctly, you want something like:
@property
def articles(self):
return Article.translations.language('en').filter(article__blog=self)
Upvotes: 1