【问题标题】:Django arrayfield search?Django数组字段搜索?
【发布时间】:2021-11-01 17:37:58
【问题描述】:

我在 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)

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

【问题讨论】:

    标签: django django-models django-views django-searchvector


    【解决方案1】:

    问题已回答here。 您可以在查询中使用它,而不是像这样保存:

    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

    【讨论】:

    • 感谢您的回答。如何将标题与搜索连接起来?
    • 我编辑了我的答案以包含它!我完全不确定这是否会起作用,因为我目前无法对其进行测试。如果您遇到错误,请在此处发布,也许我可以提供帮助。
    • 非常感谢!成功了!
    猜你喜欢
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多