Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python 2.7 - List of records not fetching updated records in Django REST framework..?

In Django REST Framework API, list of database table records are not getting updated until the API restart or any code change in python files like model, serializer or view. I've tried the transaction commit but it didn't worked. Below is my view :

class ServiceViewSet(viewsets.ModelViewSet):
    #authentication_classes = APIAuthentication,
    queryset = Service.objects.all()
    serializer_class = ServiceSerializer
    def get_queryset(self):
        queryset = self.queryset
        parent_id = self.request.QUERY_PARAMS.get('parent_id', None)
        if parent_id is not None:
           queryset = queryset.filter(parent_id=parent_id)
        return queryset   
    # Make Service readable only
    def update(self, request, *args, **kwargs): 
        return Response(status=status.HTTP_400_BAD_REQUEST)    
    def destroy(self, request, *args, **kwargs):
        return Response(status=status.HTTP_400_BAD_REQUEST)

Serializer looks like this :

class ServiceSerializer(serializers.ModelSerializer): 

    class Meta:
        model = Service
        fields = ('id', 'category_name', 'parent_id')
        read_only_fields = ('category_name', 'parent_id')

and model looks like this :

class Service(models.Model):
    class Meta:
        db_table = 'service_category'
        app_label = 'api'
    category_name = models.CharField(max_length=100)
    parent_id = models.IntegerField(default=0)
    def __unicode__(self): 
        return  '{"id":%d,"category_name":"%s"}' %(self.id,self.category_name)

This problem is occuring only with this service, rest of the APIs working perfectly fine. Any help will be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because you are setting up the queryset on self.queryset, which is a class attribute, it is being cached. This is why you are not getting an updated queryset for each request, and it's also why Django REST Framework calls .all() on querysets in the default get_queryset. By calling .all() on the queryset, it will no longer use the cached results and will force a new evaluation, which is what you are looking for.

class ServiceViewSet(viewsets.ModelViewSet):
    queryset = Service.objects.all()

    def get_queryset(self):
        queryset = self.queryset.all()
        parent_id = self.request.QUERY_PARAMS.get('parent_id', None)

        if parent_id is not None:
           queryset = queryset.filter(parent_id=parent_id)

        return queryset

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...