You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建不属于任何模型类的API函数?Django/DRF场景求助

解决方案:无需无表模型,直接编写独立API视图

嘿,我来帮你理清这个问题!你完全不需要创建无数据库表的模型——DRF提供了非常灵活的方式来编写不依赖任何模型的API视图,这正好匹配你这种第三方API转发的场景。下面给你两种实用的实现方式和代码示例:

1. 用类视图(APIView)实现

类视图适合逻辑稍复杂、后续可能需要扩展权限/节流功能的场景,结构更清晰。

步骤1:准备依赖与配置

先安装Google Cloud Vision的SDK:

pip install google-cloud-vision

然后配置服务账号密钥:绝对不要硬编码密钥,建议通过环境变量GOOGLE_APPLICATION_CREDENTIALS指向你的密钥文件(比如服务器上执行export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json")。

步骤2:编写API视图

在你的app的views.py里添加以下代码:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from google.cloud import vision_v1
from google.cloud.vision_v1 import types

class ImageTaggingView(APIView):
    def post(self, request, format=None):
        # 校验请求是否包含图片
        if 'image' not in request.FILES:
            return Response({'error': '请上传图片文件'}, status=status.HTTP_400_BAD_REQUEST)
        
        image_file = request.FILES['image']
        # 初始化Vision API客户端
        client = vision_v1.ImageAnnotatorClient()
        
        # 读取图片内容并构造请求对象
        image_content = image_file.read()
        vision_image = types.Image(content=image_content)
        
        # 调用Google标签检测接口
        api_response = client.label_detection(image=vision_image)
        labels = api_response.label_annotations
        
        # 整理返回给客户端的结果
        tag_results = [
            {'标签': label.description, '置信度': round(label.score, 2)} 
            for label in labels
        ]
        
        # 处理Google API返回的错误
        if api_response.error.message:
            return Response(
                {'error': f'Google API调用失败: {api_response.error.message}'},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )
        
        return Response({'检测结果': tag_results}, status=status.HTTP_200_OK)

步骤3:配置URL路由

在你的app的urls.py里添加路由:

from django.urls import path
from .views import ImageTaggingView

urlpatterns = [
    # 其他路由...
    path('api/detect-image-tags/', ImageTaggingView.as_view(), name='detect-image-tags'),
]

2. 用函数视图(@api_view装饰器)实现

如果逻辑简单,函数视图会更轻量化,代码更简洁:

from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from google.cloud import vision_v1
from google.cloud.vision_v1 import types

@api_view(['POST'])
def detect_image_tags(request):
    if 'image' not in request.FILES:
        return Response({'error': '请上传图片文件'}, status=status.HTTP_400_BAD_REQUEST)
    
    image_file = request.FILES['image']
    client = vision_v1.ImageAnnotatorClient()
    image_content = image_file.read()
    vision_image = types.Image(content=image_content)
    
    api_response = client.label_detection(image=vision_image)
    labels = api_response.label_annotations
    
    tag_results = [
        {'标签': label.description, '置信度': round(label.score, 2)} 
        for label in labels
    ]
    
    if api_response.error.message:
        return Response(
            {'error': f'Google API调用失败: {api_response.error.message}'},
            status=status.HTTP_500_INTERNAL_SERVER_ERROR
        )
    
    return Response({'检测结果': tag_results}, status=status.HTTP_200_OK)

对应的URL路由:

path('api/detect-image-tags/', detect_image_tags, name='detect-image-tags'),

额外的最佳实践

  • 安全加固:可以给这个API添加权限控制(比如用DRF的IsAuthenticated权限类),确保只有授权的客户端能调用。
  • 防滥用:添加节流(Throttling)规则,限制单位时间内的请求次数。
  • 友好错误:额外校验图片格式、大小,返回更精准的错误提示(比如“仅支持JPG/PNG格式”)。
  • 代码组织:如果这类第三方API调用逻辑较多,可以单独创建一个third_party_apis app来存放相关视图和工具函数,让项目结构更清晰。

内容的提问来源于stack exchange,提问作者Evan Zamir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:25:21