Django rest_framework缓存SQL查询

时间:2019-03-21 11:30:24

标签: django django-rest-framework

我是Django和React的新手。我要接管Django / React项目,并且遇到一个API网址被称为的问题:

GET /api/admin/parents/

但基本的sql语句:

from django.http import HttpResponseRedirect
from django.db.models import Q
from .models import *
from rest_framework import permissions, status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView, ListCreateAPIView, CreateAPIView, RetrieveUpdateAPIView, DestroyAPIView
from .serializers import *
from twilio.rest import Client

class AdminAllParentsView(ListAPIView):
    serializer_class = UserSerializer
    queryset = User.objects.filter(is_staff=False, is_parent=True, is_active=True)
不会在每次执行GET / api / admin / parents /调用时重新执行

例如,..更改了一条sql记录,以便与api调用/查询的is_parent = True部分匹配的记录减少了... api调用返回的结果不反映真实/当前状态查询。 GET / api / admin / parents /仍从先前的sql查询(我假定的缓存版本)返回数据,并且不会重新执行查询。

其中urls.py部分是:

from django.urls import path
from rest_framework_jwt.views import obtain_jwt_token
from .views import *

urlpatterns = [
    path('admin/parents/', AdminAllParentsView.as_view())

那么我每次进行api调用时如何从中获取当前的sql数据?

  • jack

...我在浏览器中看到/ api / admin / parents /请求熄灭并通过json数据返回旧数据。这不是缓存的浏览器html东西。

...这是用户序列化程序代码...有人说这可能也很重要。

from rest_framework import serializers
from rest_framework_jwt.settings import api_settings
from .models import *


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = (
            'id',
            'email',
            'first_name',
            'last_name',
            'is_parent',
            'phone',
            'mobile_phone',
            'address',
            'city',
            'zip',
            'county',
            'agency_name',
            'caseworker_name',
            'caseworker_email',
            'caseworker_phone',
            'caseworker_ext',
            'is_active',
            'agency_approved',
        )


class UserSerializerWithToken(serializers.ModelSerializer):

    token = serializers.SerializerMethodField()
    password = serializers.CharField(write_only=True)

    def get_token(self, obj):
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(obj)
        token = jwt_encode_handler(payload)
        return token

    def create(self, validated_data):
        password = validated_data.pop('password', None)
        instance = self.Meta.model(**validated_data)
        if password is not None:
            instance.set_password(password)
        instance.save()
        return instance

    class Meta:
        model = User
        fields = '__all__'

0 个答案:

没有答案