django - 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)
这不起作用,我该怎么做才能使它起作用?
解决方案
问题已在此处得到解答。您可以在查询中使用它,而不是像这样保存:
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
推荐阅读
- azure - 有关受保护项目 PowerShell 的 ASR 复制存储帐户详细信息
- html - 如何将 Excel 工作表的格式和全范围转换为 HTML 电子邮件正文?
- containers - 是否可以使用 ctr 命令运行带有端口转发的容器?
- angular - 如何使用硒单击角度应用程序按钮
- java - Java Swing 自定义滑块图像
- swift - 想要翻转两个视图,以便一个视图隐藏而其他视图快速显示
- c++ - 如何找到“弱”内存泄漏
- python - 导入 neupy.algorithms 时出现语法错误
- java - 如何将令牌存储到给定数组中自己的元素中?
- spring-boot - HAL 格式中的 Swagger 响应 - 春季