Home > Mobile >  Django arrayfield search?
Django arrayfield search?

Time:11-02

I have an arrayfield named 'hashtags' in game.models. I want to make search by giving title ,also giving hashtags to search bar.

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

This is my model:

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)

This is not working, what can I do to make it work?

CodePudding user response:

The question has been answered here. You can use it in a query instead of save the same way like this:

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')

Ps. you need to concat title to search aswell

  • Related