快速构建 RESTful API
Project description
Dseagull
快速构建 RESTful API
INSTALLED_APPS
添加 dseagull 到 INSTALLED_APPS 中, 注意必须要放在 rest_framework 前面
INSTALLED_APPS = [
...
'dseagull',
'rest_framework',
]
MIDDLEWARE
添加 dseagull.logger.LoggerMiddleware 到 MIDDLEWARE 中, 用于收集日志的字段
MIDDLEWARE = [
'dseagull.logger.LoggerMiddleware',
...
]
REST_FRAMEWORK
不需要配置 REST_FRAMEWORK, 默认配置如下:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'dseagull.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend',
'rest_framework.filters.SearchFilter',
'rest_framework.filters.OrderingFilter',
],
}
models.BaseModel
提供基础模型, 添加了 created, updated
from django.db import models
class BaseModel(models.Model):
class Meta:
abstract = True
created = models.DateTimeField(
auto_now_add=True, verbose_name='创建时间', db_index=True,
)
updated = models.DateTimeField(
auto_now=True, verbose_name='更新时间', db_index=True,
)
serializers.Field
支持 required=True 时提示带上字段的 help_text 信息
from rest_framework.serializers import Serializer
class ExampleSerializer(Serializer):
name = field(help_text='姓名')
ExampleSerializer(data={}).is_valid()
原本提示:这个字段是必填项。
现提示:姓名:这个字段是必填项。
支持 required=True, null=False 时提示带上字段的 help_text 信息
from rest_framework.serializers import Serializer
class ExampleSerializer(Serializer):
name = field(help_text='姓名')
ExampleSerializer(data={'name': None}).is_valid()
原本提示:This field may not be null. 现提示:姓名:不能为空。
支持 required=True, null=False 时提示带上字段的 help_text 信息
from rest_framework.serializers import Serializer
class ExampleSerializer(Serializer):
name = field(help_text='姓名')
ExampleSerializer(data={'name': ''}).is_valid()
原本提示:This field may not be blank. 现提示:姓名:不能为空白。
Filters
支持时间区间的查询
from dseagull.filters import BaseFilterSet
class PersonFilter(BaseFilterSet):
last_name = filters.CharFilter()
created = filters.CharFilter(method='filter_datetime', )
class Meta:
model = Person
fields = ('id',)
在查询时, 参数输入 /?created=1738771200,1738771201 即可过滤出对应的数据
Commands
startmodel
python manage.py startmodel -n Apple
执行上面的命令, 可以自动创建和修改标准化的 model, serializer, viewset, routers
JWT
简化对称加密型的 JWT 编码和解码的过程, 需要配置 JWT_KEY 和 JWT_EXP,
from dseagull.djwt import JWTHS256
token = JWTHS256().encode({'username': 'admin'})
payload = JWTHS256().decode(token)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dseagull-0.0.23.tar.gz.
File metadata
- Download URL: dseagull-0.0.23.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c08e9214c2684fca18ea18c689299b167d7f51b1708efbe82e24e69f1a70f58b
|
|
| MD5 |
8d5d5f99b91794038ac1c910a8337921
|
|
| BLAKE2b-256 |
77f326e1c05f98d35ddc9f8c49e8f29744cb5aef49b0986f16e7cce892c575d3
|
File details
Details for the file dseagull-0.0.23-py3-none-any.whl.
File metadata
- Download URL: dseagull-0.0.23-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f0e507af8e6faa12527497d01de9a9914642df8d2b59f58f5fdbc3e0359e749
|
|
| MD5 |
681c77c61f6b7278ebef038c09b559e3
|
|
| BLAKE2b-256 |
42cb214e2f42b6a9bc3e226626d9660cc5f1c29fcc0b50da8cacbdb3f35a1cc7
|