在那里问候!
我建立了一个自定义查询集,我注意到它的__init__()
方法被多次调用。
让我们考虑到我也在申请Queryset Chaining
模型.py
class Todo(...):
...
objects = TodoQueryset.as_manager()
管理器.py
from types import MethodType
from django.conf import settings
from django.db import models
from django.utils import timezone
from .settings import TODO_PRIORITY, TODO_STATUS, today_start_date
class TodoQueryset(models.QuerySet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.priorities = TODO_PRIORITY
print('\t weird 0')
def starting_from_today(self):
return self.filter(start_time__gte=today_start_date)
def flag_as_missed(self):
""" Flag Todos which end_time is lower than the current datetime as `TODO_MISSED` """
return self.filter(status=settings.TODO_PROGRESS, end_time__lt=timezone.now).update(status=settings.TODO_MISSED)
def specific(self, priority=None, status=None):
"""
Fetch Todos with a specific priority:
=> {self.}request.user.todos.specific(priority='HIGH')
Or call the specific method set for each priority:
=> {self.}request.user.todos.high() // NOT IMPLEMENTED YET
"""
assert priority is not None or status is not None, '.specific() must be called with either `status=...` or `priority=...`, got None instead.'
return self.filter(priority=priority.upper())
序列化程序.py
class TodoListSerializer(serializers.Serializer):
todo_id = serializers.IntegerField(read_only=True)
author_id = serializers.IntegerField(read_only=True)
content = serializers.CharField(read_only=True)
start_time = serializers.DateTimeField(read_only=True)
end_time = serializers.DateTimeField(read_only=True)
timestamp = serializers.DateTimeField(read_only=True)
priority = serializers.CharField(read_only=True)
status = serializers.CharField(read_only=True)
path = serializers.SerializerMethodField()
def get_path(self, instance):
reverse_view = reverse(viewname="todo-detail", args=[instance.pk])
request = self.context.get('request', None)
if request is not None:
return request.build_absolute_uri(reverse_view)
return reverse_view
视图.py
class TodoListView(generics.ListAPIView):
view_name = 'todo-list'
serializer_class = TodoListSerializer
permission_classes = ()
def get_queryset(self):
priority = self.request.query_params.get('priority', None)
qs = self.request.user.todos.starting_from_today()
if priority is not None:
return qs.specific(priority=priority)
return qs
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
data = {}
data['priorities'] = {
p[0].lower(): '{}?priority={}'.format(self.request.build_absolute_uri(reverse(viewname=self.view_name)), p[0] ) for p in TODO_PRIORITY
}
data['todos'] = serializer.data
return Response(data)
TodoQueryset().__init__() 打印语句的输出
System check identified no issues (0 silenced).
April 10, 2020 - 16:25:06
Django version 3.0.4, using settings 'config.sandbox'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
weird 0
weird 0
weird 0
[10/Apr/2020 16:35:31] "GET /api/todos/ HTTP/1.1" 200 6377
目前我只创建了 1 个 Todo,我有点不明白为什么它被执行了 3 次(它被执行了 5 次/api/todos/1/
)
有没有人愿意为这件事提供一些启示?