{"id":20467297,"url":"https://github.com/firebrother/dcservertutorial","last_synced_at":"2025-03-05T12:43:31.072Z","repository":{"id":87389217,"uuid":"219672313","full_name":"FireBrother/DCServerTutorial","owner":"FireBrother","description":"历时语料库后端教程项目","archived":false,"fork":false,"pushed_at":"2019-11-18T16:18:25.000Z","size":13,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-16T01:31:56.664Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FireBrother.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-05T06:20:40.000Z","updated_at":"2019-11-18T16:18:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"febf8fca-0571-4d7e-8392-03f5db7580e2","html_url":"https://github.com/FireBrother/DCServerTutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FireBrother%2FDCServerTutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FireBrother%2FDCServerTutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FireBrother%2FDCServerTutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FireBrother%2FDCServerTutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FireBrother","download_url":"https://codeload.github.com/FireBrother/DCServerTutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242031303,"owners_count":20060579,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T13:28:00.636Z","updated_at":"2025-03-05T12:43:31.065Z","avatar_url":"https://github.com/FireBrother.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 历时语料库后端教程项目\n## 初始化项目\n```shell\ndjango-admin startproject DCServerTutorial\ndjango-admin startapp entry_editor\n```\n\n## 修改settings.py\n在`INSTALLED_APPS`中加入`rest_framework`和我们刚刚创建的`entry_editor`。\n\n## 模型定义与数据库\n在`entry_editor/models.py`中加入`EntryEditor`对象的定义。\n\n```shell\npython3 manage.py makemigrations\npython3 manage.py migrate\n```\n\n此时我们使用的是sqlite作为数据库后端。如果想要采用其他数据库（比如mysql），需要在settings.py中做相应的改动。\n\n## Serializer定义\n默认的Django环境是没有Serializer这个概念的，所以我们需要手动创建这个文件。\n\n因为我们希望在List和Retrieve时采用不同的序列化器，所以我们创建了两个。分别是`EntryEditorSerializer`和`EntryEditorListSerializer`。\n\n区别在于，前者对`cells`中的内容也做了序列化，而后者无需显示这个字段。\n\n## ViewSet定义\n在`entry_editor/views.py`中定义一个视图，用来完成我们对EntryEditor的各种操作。\n\n在目前的简化版系统中，我们只需要支持create, retrieve, list, update和destroy动作就可以了。\n\n不用担心，这些动作都是在ModelViewSet中定义好的，我们只需要继承这个ViewSet，并且重写一些业务逻辑即可。\n\n在这里我们还加入了简单的对于filter、queryset、serializer_class的设置。\n\n## 简单说下View\n在上一步，我们直接跳过了原生Django中对view的定义，以及对GenericViewSet的介绍。在这里还是做一个简单的补充。\n\n简单来说，一个View就对应着我们访问的一个（多个）HTTP动词+一个URL pattern。而由于RESTful API对于资源和动词的定义，只要我们的业务是符合框架的定义的，就可以采用默认的view来简化开发。\n\n可以看到，在ViewSet中，我们先通过成员变量将一个viewset与一个model建立起了联系，也与一个（多个）序列化器建立了联系。这样一来，ModelViewSet将会为我们加入许多默认的动作。（django rest framework是通过mixin的方式扩展一个基类的动作的）。\n\n## URL注册\n最后，我们只需要将viewset注册进来就可以访问了。\n\n为了单独管理每个app的url，我们首先在`entry_editor`中再创建一个`urls.py`，并将viewset注册进来。\n\n最后在全局的`urls.py`中将每个app的url再引入进来就可以了。\n\n## 运行测试服务器\n\n```shell\npython3 manage.py runserver\n```\n在默认端口8000可以访问到我们的网站了。但是因为我们没有主页，所以返回的是404的页面。因为是开发模式，我们可以看到所有合法的URL，访问entryeditor即可。\n\nDRF为我们提供了一个简单的可视化页面用来查看各种API并且测试它们。\n\n## 小结\n这一次我们创建了一个最简化的系统。遵循这个教程走下来，你可以对DRF是如何将网络请求与数据库操作建立起联系的。\n\n后续我们将会介绍更多内容，包括自定义动作、登录设置、权限认证、前后端协同、swagger的使用等。","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirebrother%2Fdcservertutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffirebrother%2Fdcservertutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirebrother%2Fdcservertutorial/lists"}