Skip to content

Commit 2f075b6

Browse files
committed
了解和学习drf_haystack
0 parents  commit 2f075b6

35 files changed

+334
-0
lines changed

DRFHTutorial/__init__.py

Whitespace-only changes.
159 Bytes
Binary file not shown.
2.47 KB
Binary file not shown.
992 Bytes
Binary file not shown.
572 Bytes
Binary file not shown.

DRFHTutorial/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for DRFHTutorial project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DRFHTutorial.settings')
15+
16+
application = get_asgi_application()

DRFHTutorial/settings.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
"""
2+
Django settings for DRFHTutorial project.
3+
4+
Generated by 'django-admin startproject' using Django 3.2.5.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.2/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/3.2/ref/settings/
11+
"""
12+
13+
from pathlib import Path
14+
15+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
16+
BASE_DIR = Path(__file__).resolve().parent.parent
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = 'django-insecure-p1f_6rnc2rq3ikbo#jyj4tu0mpxw(6@gt!xj!9@^zyf_dsz-47'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
'rest_framework',
41+
'haystack',
42+
'app.apps.AppConfig',
43+
]
44+
45+
MIDDLEWARE = [
46+
'django.middleware.security.SecurityMiddleware',
47+
'django.contrib.sessions.middleware.SessionMiddleware',
48+
'django.middleware.common.CommonMiddleware',
49+
'django.middleware.csrf.CsrfViewMiddleware',
50+
'django.contrib.auth.middleware.AuthenticationMiddleware',
51+
'django.contrib.messages.middleware.MessageMiddleware',
52+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
53+
]
54+
55+
ROOT_URLCONF = 'DRFHTutorial.urls'
56+
57+
TEMPLATES = [
58+
{
59+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
60+
'DIRS': [],
61+
'APP_DIRS': True,
62+
'OPTIONS': {
63+
'context_processors': [
64+
'django.template.context_processors.debug',
65+
'django.template.context_processors.request',
66+
'django.contrib.auth.context_processors.auth',
67+
'django.contrib.messages.context_processors.messages',
68+
],
69+
},
70+
},
71+
]
72+
73+
WSGI_APPLICATION = 'DRFHTutorial.wsgi.application'
74+
75+
76+
# Database
77+
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
78+
79+
DATABASES = {
80+
'default': {
81+
'ENGINE': 'django.db.backends.sqlite3',
82+
'NAME': BASE_DIR / 'db.sqlite3',
83+
}
84+
}
85+
86+
87+
# Password validation
88+
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
89+
90+
AUTH_PASSWORD_VALIDATORS = [
91+
{
92+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
93+
},
94+
{
95+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
96+
},
97+
{
98+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
99+
},
100+
{
101+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
102+
},
103+
]
104+
105+
106+
# Internationalization
107+
# https://docs.djangoproject.com/en/3.2/topics/i18n/
108+
109+
LANGUAGE_CODE = 'en-us'
110+
111+
TIME_ZONE = 'UTC'
112+
113+
USE_I18N = True
114+
115+
USE_L10N = True
116+
117+
USE_TZ = True
118+
119+
120+
# Static files (CSS, JavaScript, Images)
121+
# https://docs.djangoproject.com/en/3.2/howto/static-files/
122+
123+
STATIC_URL = '/static/'
124+
125+
# Default primary key field type
126+
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
127+
128+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
129+
130+
HAYSTACK_CONNECTIONS = {
131+
"default": {
132+
# For Simple:
133+
"ENGINE": "haystack.backends.simple_backend.SimpleEngine"
134+
},
135+
}

DRFHTutorial/urls.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""DRFHTutorial URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/3.2/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path, include
18+
19+
urlpatterns = [
20+
path('admin/', admin.site.urls),
21+
path('api/v1/', include('app.urls'))
22+
]

DRFHTutorial/wsgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for DRFHTutorial project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DRFHTutorial.settings')
15+
16+
application = get_wsgi_application()

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# DRFHTutorial
2+
3+
学完django, djangorestframework, django-haystack之后我发现,django-haystack还停留在
4+
编写模版的阶段,也就是说不是前后端分离的,然后我开始思考如何将django-haystack与DRF集成,幸运
5+
的是在我准备实现之前我发现了这个开源项目[drf_haystack](https://drf-haystack.readthedocs.io/en/latest/01_intro.html#examples).
6+
它看起来很好,我决定开始了解和学习,然后在以后的项目中使用上。
7+
8+
但是,实际根据我使用的结果没有出现数据,或者说有些数据已经丢失了。
9+
10+
我增加两个数据到数据库中:
11+
12+
![](https://github.com/cs246810/DRFHTutorial/blob/master/create_data.png)
13+
14+
接着创建索引。
15+
16+
然后,测试api搜索:
17+
18+
![](https://github.com/cs246810/DRFHTutorial/blob/master/api_test.png)
19+
20+
结果有点差强人意,确实出现了结果,但是,这里出现的说text为null的数据,显然我没有找到正确的使用drf_haystack
21+
的方法。

0 commit comments

Comments
 (0)