简体   繁体   English

如何在我的数据库中添加数据以使用 django 和 DRF

[英]how to add data in my db for using django and DRF

I'm producing an API that uses DRF to receive Arduino's data and send it to Android.我正在生产一个 API,它使用 DRF 接收 Arduino 的数据并将其发送到 Android。 First of all, we succeeded in creating an API that shows the values stored in DB.首先,我们成功创建了一个 API,它显示了存储在 DB 中的值。 However, there is a problem in producing an API that sends Arduino's data to DB.但是,在生产将 Arduino 的数据发送到 DB 的 API 时存在问题。 Data is normally input, but this data is not stored.通常输入数据,但不存储该数据。 Can you tell me the problem of my code?你能告诉我我的代码的问题吗? here is my codes这是我的代码

views.py视图.py

from .models import arduino
from .serializers import arduinoSerializers
from rest_framework.viewsets import ViewSet, ModelViewSet
from rest_framework.response import Response

class arduinoToAndroidViewSet (ViewSet) :
    def dataSend (self, request) :
        user = self.request.user
        queryset = arduino.objects.filter(name=user)
        serializer = arduinoSerializers(queryset, many=True)
        return Response(serializer.data)

class arduinoToDatabaseViewSet (ModelViewSet) :
    serializer_class = arduinoSerializers

    def get_queryset(self) :
        user = self.request.user
        return arduino.objects.filter(name=user)

    def dataReceive(self, request) :
        queryset = get_queryset()
        serializer = arduinoSerializers(queryset, many=True)
        serializer.save()
        return Response(serializer.data)

serializers.py序列化程序.py

from rest_framework import serializers
from .models import arduino

class arduinoSerializers (serializers.ModelSerializer) :
    name = serializers.CharField(source='name.username', read_only=True)
    class Meta :
        model = arduino
        fields = ('name', 'temp', 'humi')

models.py模型.py

from django.db import models
from django.contrib.auth.models import User

class arduino (models.Model) :
    name = models.ForeignKey(User, related_name='Username', on_delete=models.CASCADE, null=True)
    temp = models.FloatField()
    humi = models.FloatField()

    def __str__ (self) :
        return self.name.username

If you are referring to the problem that your dataReceive(self, request) method is not saving data despite of having a valid queryset (could not understand why you are querying and saving the save object again), is because you have to always call is_valid() before attempting to save.如果您指的是尽管有有效的查询集但您的queryset dataReceive(self, request)方法没有保存数据的问题(不明白为什么要再次查询并保存保存 object),是因为您必须始终调用is_valid()在尝试保存之前。 so it should be所以应该是

def dataReceive(self, request) :
    queryset = get_queryset()
    serializer = arduinoSerializers(queryset, many=True)
    if serializer.is_valid():
        serializer.save()
    return Response(serializer.data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据添加到 Django DRF 中的多对多字段 - How to add data to Many-To-Many field in Django DRF 如何使用 django 将多行数据添加到 postgresql db - how to add the multiple row data into postgresql db using django Django DRF:如何显示过滤后的数据 - Django DRF: How to show filtered data 如何在DRF中使用django-taggit - How to using django-taggit in DRF 如何使用 Django DRF viewset.Viewsets 添加自定义操作 function? - How to add a custom action function using Django DRF viewset.Viewsets? 我正在为我的 Django rest api 文档使用 drf-spectacular,如何覆盖我们自己的端点 - Am using drf-spectacular for my Django rest api documentation, how to override our own endpoint 如何在不使用 model 的情况下将数据从 Django 模板(提交后)传递到 DRF API? - How to pass data from Django template (after submission) to DRF API without using a model? 在使用Django固定时间间隔后,如何更新我的数据库数据(mongodb)? - how do i update my db data(mongodb) after fixed interval of time using django? Django DRF串行器,如何添加新字段,融合两个模型? - Django DRF serializers, how to add new field, fusion of two models? 如何在 DRF 中使用 ForiegnKey 关系创建 Db 表 - how can i create Db table using ForiegnKey relationship in DRF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM