<table class="text">
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 名称 },} # 当添加、修改、删除数据时,自动生成索引 HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor' # 创建search_index.py索引文件 from haystack import indexes from goods.models import SKU class SKUIndex(indexes.SearchIndex,indexes.Indexable): """ SKU索引数据模型类 """ text = indexes.CharField(document=True,use_template=True) def get_model(self): """返回建立索引的模型类""" return SKU def index_queryset(self,using=None): """返回要建立索引的数据查询集""" return self.get_model().objects.filter(is_launched=True) 具体在templates/search/indexes/goods/sku_text.txt文件中定义 {{ object.name }} {{ object.caption }} {{ object.id }} 手动生成索引 python manage.py rebuild_index #创建序列化器 from drf_haystack.serializers import HaystackSerializer class SKUSerializer(serializers.ModelSerializer): """ SKU序列化器 """ class Meta: model = SKU fields = ('id','name','price','default_image_url','comments') class SKUIndexSerializer(HaystackSerializer): """ SKU索引结果数据序列化器 """ object = SKUSerializer(read_only=True) class Meta: index_classes = [SKUIndex] fields = ('text','object') # 创建视图函数 from drf_haystack.viewsets import HaystackViewSet class SKUSearchViewSet(HaystackViewSet): """ SKU搜索 """ index_models = [SKU] serializer_class = SKUIndexSerializer # 定义路由 router = DefaultRouter() router.register('skus/search',views.SKUSearchViewSet,base_name='skus_search') ... urlpatterns += router.urls #测试 http://api.meiduo.site:8000/skus/search/?text=wifi 原文链接:https://www.f2er.com/note/411545.html