首页 > 解决方案 > Django数组字段搜索?

问题描述

我在 game.models 中有一个名为“hashtags”的数组字段。我想通过给出标题进行搜索,也给搜索栏提供主题标签。

object_list = Oyunlar.objects.annotate(search=SearchVector('title','hashtags')).filter(search=query).order_by('-click_count')

这是我的模型:

class Oyunlar(models.Model):
    game_id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=10000)
    youtube_link=models.URLField(blank=True,null=True)
    video_aciklamasi=models.TextField(blank=True,null=True)
    platform = models.CharField(max_length=10)
    image = models.CharField(max_length=255, blank=True, null=True)
    release_date = models.DateField(blank=True, null=True)
    click_count = models.IntegerField(default=0)
    categories=models.ManyToManyField(Kategoriler,through='OyunlarKategoriler')
    base_price=models.DecimalField(default=0,max_digits=65535, decimal_places=2)
    big_discount=models.BooleanField(default=False)
    en_ucuz = models.DecimalField(default=0,max_digits=65535, decimal_places=2)
    popularite = models.IntegerField(blank=True, null=True,default=0)
    discount_rate = models.DecimalField(default=0,max_digits=65535, decimal_places=2)
    title_edit = models.CharField(max_length=500, blank=True, null=True)
    description = models.CharField(max_length=100000, blank=True, null=True)
    steam_id = models.CharField(max_length=1000, blank=True, null=True)
    metacritic = models.FloatField(blank=True, null=True)
    recommendation = models.BigIntegerField(blank=True, null=True)
    full_game = models.BooleanField(blank=True, null=True)
    age = models.CharField(max_length=500, blank=True, null=True)
    minimum = models.CharField(max_length=10000, blank=True, null=True)
    recommended = models.CharField(max_length=10000, blank=True, null=True)
    developer = models.CharField(max_length=500, blank=True, null=True)
    publisher = models.CharField(max_length=500, blank=True, null=True)
    oyun_foto = ArrayField(models.CharField(max_length=10000, blank=True, null=True),blank=True,null=True) # This field type is a guess.
    windows = models.BooleanField(blank=True, null=True)
    mac = models.BooleanField(blank=True, null=True)
    linux = models.BooleanField(blank=True, null=True)
    gamehunterz_yorumu = models.CharField(max_length=100000, blank=True, null=True)
    slugyap = AutoSlugField(default=None,null=True,populate_from='title_and_id',editable=True,max_length=10000)
    platformurl=AutoSlugField(default=None,null=True,editable=True,max_length=10000)
    hashtags = ArrayField(models.CharField(max_length=500, blank=True, null=True), blank=True, null=True)

这不起作用,我该怎么做才能使它起作用?

标签: djangodjango-modelsdjango-viewsdjango-searchvector

解决方案


问题已在此处得到解答。您可以在查询中使用它,而不是像这样保存:

from django.db.models import F, Func, Value, Concat, CharField

object_list = Oyunlar.objects.annotate(
    search=SearchVector(
        Concat(
           Func(F('hashtags'), Value(' '), function='array_to_string'),
           Value(' '),
           F('title'),
           output_field=CharField()
        )
    )
).filter(
   search=query
).order_by('-click_count')

附言。你还需要title连接search


推荐阅读