Django-filter: ๋ˆ„๊ตฌ๋“ ์ง€ Django/Postgres ๊ธฐ๋ณธ ์ „์ฒด ํ…์ŠคํŠธ ๊ฒ€์ƒ‰ ์—”์ง„๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์˜ˆ์ œ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

์— ๋งŒ๋“  2019๋…„ 02์›” 16์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: carltongibson/django-filter

์—ฌ๊ธฐ ์™„์ „ ๋…ธ์žผ....

https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/search/#postgresql -fts-search-configuration

Documentation

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์•ˆ๋…•ํ•˜์„ธ์š” @gotexis์ž…๋‹ˆ๋‹ค. django-filter๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‘ ๊ฐ€์ง€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ์ž…๋ ฅ ํ™•์ธ
  • ํ•ด๋‹น ์ž…๋ ฅ์—์„œ ์ฟผ๋ฆฌ ๊ตฌ์„ฑ

์ฐพ๊ณ  ์žˆ๋Š” ์ฟผ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” Filter ํด๋ž˜์Šค๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€ ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ƒˆ ํ•„ํ„ฐ ํด๋ž˜์Šค ์ž‘์„ฑ
  • Filter.method ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

์ฟผ๋ฆฌ๋ฅผ ์ผ๋ฐ˜ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์‚ฌ์šฉ์ž ์ •์˜ Filter ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋” ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ Filter.method ๋Š” ์ผํšŒ์„ฑ ์ธ์Šคํ„ด์Šค์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

from django.contrib.postgres.search import SearchQuery, SearchVector
from djanog_filters import FilterSet, CharFilter

class F(FilterSet):
    # The model `field_name` is the field you want to search on and is passed to your `method`.
    # The `method` argument is the name of the method to call to perform filtering.
    search = CharFilter(field_name='body_text', method='search_fulltext')

    def search_fulltext(self, queryset, field_name, value):
        return queryset \
            .annotate(search=SearchVector(field_name)) \
            .filter(search=SearchQuery(value))

๋ชจ๋“  4 ๋Œ“๊ธ€

์•ˆ๋…•ํ•˜์„ธ์š” @gotexis์ž…๋‹ˆ๋‹ค. django-filter๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‘ ๊ฐ€์ง€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ์ž…๋ ฅ ํ™•์ธ
  • ํ•ด๋‹น ์ž…๋ ฅ์—์„œ ์ฟผ๋ฆฌ ๊ตฌ์„ฑ

์ฐพ๊ณ  ์žˆ๋Š” ์ฟผ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” Filter ํด๋ž˜์Šค๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€ ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ƒˆ ํ•„ํ„ฐ ํด๋ž˜์Šค ์ž‘์„ฑ
  • Filter.method ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

์ฟผ๋ฆฌ๋ฅผ ์ผ๋ฐ˜ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์‚ฌ์šฉ์ž ์ •์˜ Filter ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋” ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ Filter.method ๋Š” ์ผํšŒ์„ฑ ์ธ์Šคํ„ด์Šค์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

from django.contrib.postgres.search import SearchQuery, SearchVector
from djanog_filters import FilterSet, CharFilter

class F(FilterSet):
    # The model `field_name` is the field you want to search on and is passed to your `method`.
    # The `method` argument is the name of the method to call to perform filtering.
    search = CharFilter(field_name='body_text', method='search_fulltext')

    def search_fulltext(self, queryset, field_name, value):
        return queryset \
            .annotate(search=SearchVector(field_name)) \
            .filter(search=SearchQuery(value))

์™€์šฐ ๋„์›€ ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค @rpkilby , ์ด ์ฝ”๋“œ๋ฅผ ์†Œํ™”ํ•˜๋Š” ๋ฐ ๋ฉฐ์น  ๋” ์‹œ๊ฐ„์„ ์ฃผ์„ธ์š”...

์ง€๊ธˆ ์ €๋Š” django-filters์™€ ํ†ตํ•ฉ๋˜๋Š” graphene-django๋กœ ๋จธ๋ฆฌ๋ฅผ ๋ถ€๋”ช์น˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋‚˜๋Š” ์ด๊ฒƒ์„ GraphQL๋กœ (์ด์ƒ์ ์œผ๋กœ) ๊ตฌํ˜„ํ•˜๊ธฐ๋ฅผ ํฌ๋งํ•ฉ๋‹ˆ๋‹ค.

graphene-django๊ฐ€ django-filter๋ฅผ ๋ž˜ํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ: #927 ์ฐธ์กฐ

@rpkilby ๋„ค ์„ ์ƒ๋‹˜
์‹ค์ œ๋กœ ๋…ธ๋“œ์— ๋น„ํ•ด ์ธ๊ธฐ๊ฐ€์—†๋Š” ํŒŒ์ด์ฌ + GraphQL์€์ด ์–ผ๋งˆ๋‚˜ ์ฃผ์–ด์ง„ ๊ทธ๋ž˜ ํ•€ ๋ฌธ์ œ๋กœ ์ธํ•ด์ด ํž™์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ํ”Œ๋Ÿฌ์Šค ์ €์ž๋Š” ๋‹น์‹ ์ด ๋™์˜ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค,์ด ํ”„๋กœ์ ํŠธ์— ํฌ๊ธฐํ•œ๋‹ค :)

django-filter์™€ ๊ด€๋ จ๋œ ๊ฐ€์žฅ ์ตœ๊ทผ ๊ฒƒ ์ค‘ ํ•˜๋‚˜๋Š” DjangoFilterConnectionField๊ฐ€ ํ”„๋ฆฌํŽ˜์นญ๊ณผ ํ•จ๊ป˜ ์ž‘๋™ํ•˜์ง€ ์•Š์•„ ์ˆ˜๋งŽ์€ ์ฟผ๋ฆฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋””๋ฒ„๊น…์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ณต์œ ํ•  ๊ฒฝํ—˜์ด ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค. :)

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰