我是 Django Rest Framework 的新手。使用 serializer和views 简单的CRUD很容易。当逻辑增加时,在serializeror 中写逻辑的地方很混乱views。
一些开发人员确实更喜欢“厚序列化器和薄视图”,而一些开发人员更喜欢“厚视图和薄序列化器”。
也许这不是一个大问题,我认为是否要在views或上写更多内容取决于开发人员serializer,但是作为新手,您有什么建议可以遵循?我应该写更多views还是serializer?
在 Django View Template 上有很多答案,但对于 Django Rest Framework 找不到令人满意的答案。
经验丰富的开发人员的想法将受到高度赞赏。谢谢你。
django rest serialization django-views django-rest-framework
有一个特定的周期性任务需要从消息队列中删除。我这里使用的是Redis和celery的配置。
任务.py
@periodic_task(run_every=crontab(minute='*/6'))
def task_abcd():
"""
some operations here
"""
Run Code Online (Sandbox Code Playgroud)
项目中还有其他定期任务,但我需要从现在开始停止此特定任务。
正如这个答案中所解释的,下面的代码可以工作吗?
@periodic_task(run_every=crontab(minute='*/6'))
def task_abcd():
pass
Run Code Online (Sandbox Code Playgroud) 我正在尝试为一个虚拟的本地 Django 项目实现一个 docker。我docker-compose用作定义和运行多个容器的工具。在这里,我尝试将Django-web-app和PostgreSQL两个服务容器化。
中使用的配置Dockerfile和docker-compose.yml
文件
# Pull base image
FROM python:3.7-alpine
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY requirements.txt /code/
RUN pip install -r requirements.txt
# Copy project
COPY . /code/
Run Code Online (Sandbox Code Playgroud)
docker-compose.yml
version: '3.7'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres:11
volumes:
- …Run Code Online (Sandbox Code Playgroud) celery中不同时期运行的任务一共有8个。所有这些都是事件驱动的任务。某次事件发生后,他们被解雇了。并且特定任务会持续进行,直到满足某些条件为止。
我已经注册了一项任务,该任务会检查某些条件近两分钟。这个任务在大多数情况下都可以正常工作。但有时任务的预期行为并未达到。
任务签名如下:
任务.py
import time
from celery import shared_task
@shared_task()
def some_celery_task(a, b):
main_time_end = time.time() + 120
while time.time() < main_time_end:
...
# some db operations here with given function arguments 'a' and 'b'
# this part of the task get execute most of the time
if time.time() > main_time_end:
...
# some db operations here.
# this part is the part of the task that doesn't get executed sometimes
Run Code Online (Sandbox Code Playgroud)
视图.py
# the other part of the view …Run Code Online (Sandbox Code Playgroud) django ×4
python ×3
celery ×2
celery-task ×1
django-views ×1
docker ×1
dockerfile ×1
python-3.6 ×1
redis ×1
rest ×1