1

コード:

class Article(models.Model):
    hits              = models.IntegerField(help_text = 'Visits count')
    answer_to_article = models.ForeignKey('self', blank = True, null = True)
    slug              = models.SlugField(unique = True, help_text = 'Address')
    meta_keywords     = models.CharField(max_length = 512)
    title             = models.CharField(max_length = 256)
    content           = models.TextField(verbose_name = 'Article contents', help_text = 'Article contents')

    def get_similar_articles_from_meta_and_relation(self, phrase, offset = 0, limit = 10):
        return ArticleToArticle.objects.find(article_one)[offset:limit]

    class Meta:
        db_table = 'article'

#only a relational table - one article can have similar articles chosen by it's author
class ArticleToArticle(models.Model):
    article_one = models.ForeignKey(Article, related_name = 'article_source')
    article_two = models.ForeignKey(Article, related_name = 'article_similar')

    class Meta:
        db_table = 'article_to_article'

私の質問は、Articleモデルのget_similar_articles_from_meta_and_relationメソッドについてです-次のことを行います:1。インスタンスに接続されている他の記事を検索します2.指定されたフレーズ(meta_keywords)に従ってそれらをフィルタリングします前者には問題はありませんが、後で問題があります。

4

1 に答える 1

1

meta_keywordsとphraseパラメータの関係はわかりませんが、次のようなものが必要になる可能性があります。

class Article(models.Model):
    hits              = models.IntegerField(help_text = 'Visits count')
    answer_to_article = models.ForeignKey('self', blank = True, null = True)
    slug              = models.SlugField(unique = True, help_text = 'Address')
    meta_keywords     = models.CharField(max_length = 512)
    title             = models.CharField(max_length = 256)
    content           = models.TextField(verbose_name = 'Article contents', help_text = 'Article contents')
    similar_articles  = models.ManyToMany('self')

    def get_similar_articles_from_meta_and_relation(self, phrase, offset = 0, limit = 10):
        return self.similar_articles.filter(meta_keywords=phrase)[offset:limit]

    class Meta:
        db_table = 'article'
于 2012-09-25T19:18:58.487 に答える