【问题标题】:Saving data to Postgres DB using django rest framework使用 django rest 框架将数据保存到 Postgres DB
【发布时间】:2023-03-19 03:38:01
【问题描述】:

我是 django 开发的新手。我正在开发一个 API,我在其中从客户端(移动应用程序)发送数据,并且这些数据将使用 django 存储在数据库中。如果我查询数据,则应从数据库中获取数据。数据库最好是 postgres/mysql DB。我已经编写了部分代码,但卡住了如何继续。如果有人能指导我如何继续,我将不胜感激。

from django.shortcuts import render
from rest_framework.views import APIView
from django.http import Http404
from django.http import JsonResponse
from django.core import serializers
from django.conf import settings
import json

# Create your views here. 
@api_view(["POST"])
def getIdealWeight(heightData): 
    try:
        height=json.loads(heightData.body)
        weight=str(height*10)
        return JsonResponse("the ideal weight is:"+weight+" kg.",safe=False) 
    except ValueError as e:                  
        return Response(e.args[0],status.HTTP_400_BAD_REQUEST)

【问题讨论】:

    标签: python postgresql api web-services django-rest-framework


    【解决方案1】:

    从代码来看,我建议您从 DRF Tutorials 的开头开始,并在继续您的项目之前覆盖其中的大部分内容。在快速入门之后,您应该访问专用于主要概念的各个部分,例如序列化程序、views 等。在不充分了解框架的情况下直接进入项目将很难实现您想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2015-12-10
      • 2017-12-31
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多