You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Django实现关联trt_text与trt_keyconcepts的高级搜索?

解决方案

1. 必须创建forms.py

forms.py能帮你规范前端搜索/排序参数的接收、验证,还能快速渲染表单控件,是实现高级搜索和排序的最佳实践,避免直接处理request.GET参数的混乱。

2. 分步实现步骤

第一步:修正模型,建立表关联

先在models.py中给两张表建立外键关联(一个trt_text对应多个trt_keyconcepts,外键放在trt_keyconcepts侧):

# models.py
from django.db import models

class TrtText(models.Model):
    title = models.CharField(max_length=255, db_column='title')  # db_column对应数据库实际字段名
    content = models.TextField(db_column='content')

    def __str__(self):
        return self.title

class TrtKeyconcepts(models.Model):
    text = models.ForeignKey(TrtText, on_delete=models.CASCADE, related_name='keyconcepts', db_column='text_id')
    term = models.CharField(max_length=255, db_column='term')  # 对应你说的"首列词汇/语句"
    type = models.CharField(max_length=100, db_column='type')
    frequency = models.IntegerField(db_column='frequency')

    def __str__(self):
        return self.term

执行迁移命令同步数据库:

python manage.py makemigrations
python manage.py migrate

第二步:创建搜索排序表单forms.py

定义接收关键词和排序选项的表单:

# forms.py
from django import forms

class TextSearchSortForm(forms.Form):
    keyword = forms.CharField(
        label='关键词',
        required=False,
        widget=forms.TextInput(attrs={'placeholder': '输入关键词过滤'})
    )
    sort_option = forms.ChoiceField(
        label='排序方式',
        choices=[
            ('title', '标题正序'),
            ('-title', '标题倒序'),
            ('keyconcepts__frequency', '关键词频次正序'),
            ('-keyconcepts__frequency', '关键词频次倒序'),
        ],
        initial='title'
    )

第三步:修改views.py实现过滤与排序逻辑

整合表单、关联查询、排序逻辑:

# views.py
from django.shortcuts import render
from .models import TrtText
from .forms import TextSearchSortForm

def text_search(request):
    form = TextSearchSortForm(request.GET)
    texts = TrtText.objects.all()

    if form.is_valid():
        keyword = form.cleaned_data.get('keyword')
        sort_by = form.cleaned_data.get('sort_option')

        # 通过关联的keyconcepts.term过滤文本
        if keyword:
            texts = texts.filter(keyconcepts__term__icontains=keyword).distinct()
        
        # 执行排序
        if sort_by:
            texts = texts.order_by(sort_by)

    return render(request, 'text_search.html', {'texts': texts, 'form': form})
  • distinct()用于避免同一文本因多个匹配关键词重复显示
  • keyconcepts__term__icontains是Django的关联查询语法,实现跨表模糊匹配

第四步:编写前端模板text_search.html

渲染表单和结果,实现交互:

<!-- templates/text_search.html -->
<h1>文本搜索与排序</h1>
<form method="get">
    {{ form.as_p }}
    <button type="submit">应用筛选</button>
</form>

<div class="results">
    {% if texts %}
        <ul>
            {% for text in texts %}
                <li>
                    <h3>{{ text.title }}</h3>
                    <p>{{ text.content|truncatechars:200 }}</p>
                    <p>关联关键词:
                        {% for concept in text.keyconcepts.all %}
                            {{ concept.term }} (类型:{{ concept.type }},频次:{{ concept.frequency }}){% if not forloop.last %}, {% endif %}
                        {% endfor %}
                    </p>
                </li>
            {% endfor %}
        </ul>
    {% else %}
        <p>未找到匹配内容</p>
    {% endif %}
</div>

第五步:配置urls.py

添加对应路由:

# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('text-search/', views.text_search, name='text_search'),
]

额外注意事项

  • 如果数据库原字段名和模型定义不一致,必须用db_column参数指定对应关系
  • 若需更复杂的过滤(比如按关键词类型筛选),可在forms.py中添加ChoiceField,并在views.py中补充filter(keyconcepts__type=xxx)逻辑
  • 前端无刷新排序可通过JS实现,但后端排序对新手更友好,且支持URL参数留存筛选状态

内容的提问来源于stack exchange,提问作者Broady

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 05:12:31