场景:每次调用接口,自动更新某个字段
基于Django REST framework
class ApiDataTrackingView(ModelViewSet):
queryset = ApiDataTracking.objects.all()
serializer_class = ApiDataTrackingSerializers
filterset_class = ApiDataTrackingFilter
ordering_fields = ['id', 'run_date', 'run_success_rate', 'interface_coverage', 'regression_case_coverage']
def get_queryset(self):
""" 更新test_manager字段 """
queryset = super().get_queryset()
for obj in queryset:
obj.test_manager = Product.objects.get(name=obj.product.name).test_manager
obj.save(update_fields=['test_manager']) # 只更新test_manager字段
return queryset
@auth
@CatchExceptionLog
@Log('GET', '获取数据')
def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
挺好用的,但是当数据量大的时候会有性能问题,可以考虑批量查询更新或更好的方式