如何从我的serializer.py文件访问任何特定字段?

时间:2019-04-03 04:21:48

标签: django serialization django-models django-rest-framework django-views

我正在为网络挂钩创建API。当我尝试仅从models.py中的班级消息访问一个字段时遇到问题。

Models.py

from django.db import models
from django.utils import timezone

from django_hstore import hstore


class WebhookTransaction(models.Model):
    UNPROCESSED = 1
    PROCESSED = 2
    ERROR = 3

    STATUSES = (
        (UNPROCESSED, 'Unprocessed'),
        (PROCESSED, 'Processed'),
        (ERROR, 'Error'),
    )

    date_generated = models.DateTimeField()
    date_received = models.DateTimeField(default=timezone.now)
    body = hstore.SerializedDictionaryField()
    request_meta = hstore.SerializedDictionaryField()
    status = models.CharField(max_length=250, choices=STATUSES, default=UNPROCESSED)

    objects = hstore.HStoreManager()

    def __unicode__(self):
        return u'{0}'.format(self.date_event_generated)


class Message(models.Model):
    team_id = models.CharField(max_length=250)
    team_domain = models.CharField(max_length=250)
    channel_id = models.CharField(max_length=250)
    channel_name = models.CharField(max_length=250)
    user_id = models.CharField(max_length=250)
    user_name = models.CharField(max_length=250)
    text = models.TextField()
    trigger_word = models.CharField(max_length=250)

    def __unicode__(self):
        return u'{}'.format(self.user_name)

serializer.py

from rest_framework import serializers
from slack.models import WebhookTransaction, Message


class WebhookTransactionSerializer(serializers.ModelSerializer):
    class Meta:
        model = WebhookTransaction
        fields = '_all__'



class MessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Message
        fields = ['team_id']

现在,当我尝试仅从模型中访问“ team_id”时,就会出现此错误

return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "slack_message" does not exist
LINE 1: INSERT INTO "slack_message" ("team_id", "team_domain", "chan...
                    ^

现在我的问题是我是否必须通过所有领域才能工作? 如果不是,那么我怎么只能从列表中访问一个字段?

为了给您更多的见解,这里是我的views.py代码

from rest_framework.views import APIView
from django.http import HttpResponse, JsonResponse
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt
from slack.models import WebhookTransaction
from slack.serializers import WebhookTransactionSerializer
from slack.models import Message
from slack.serializers import MessageSerializer
from rest_framework.response import Response
import requests



class DRSWebhookTransactionView(APIView):
    def post(self, request, *args, **kwargs):
        if request.method == 'GET':
            webhooktransaction = WebhookTransaction.objects.all()
            serializer = WebhookTransactionSerializer(webhooktransaction, many=True)
            return JsonResponse(serializer.data, safe=False)

        elif request.method == 'POST':
            data = JSONParser().parse(request)
            serializer = WebhookTransactionSerializer(data=data)
            if serializer.is_valid():
                serializer.save()
                return JsonResponse(serializer.data, status=201)
            return JsonResponse(serializer.errors, status=400)


class DRSMessageView(APIView):
    def post(self, request, *args, **kwargs):
        if request.method == 'GET':
            message = Message.objects.all()
            serializer = MessageSerializer(message, many=True)
            return JsonResponse(serializer.data, safe=False)

        elif request.method == 'POST':
            data = JSONParser().parse(request)
            serializer = MessageSerializer(data=data)
            if serializer.is_valid():
                serializer.save()
                return JsonResponse(serializer.data, status=201)
            return JsonResponse(serializer.errors, status=400)

您能看看我在做什么错吗? 还要尝试解释后端的工作原理

0 个答案:

没有答案