0
我正在構建自己的自定義管理器來獲取模型的翻譯。的理念是:Django自定義管理器 - 如何在QuerySet之前使用自定義管理器
主要型號:
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()
和模板,我希望能夠調用:
{{ blog.articles }}
我所描述的問題吧上面的代碼中,任何想法吧?任何幫助,高度讚賞!
這看起來不錯,讓我給一個鏡頭 – doniyor
我以不同的方式解決,但你的回答是踢球.. – doniyor