I am sending JSON via POST to a view. The response I get back is only part JSON and the rest has changed from double quotes to single quotes.
我通过POST将JSON发送到视图。我得到的响应只是JSON的一部分,其余部分已经从双引号改为单引号。
My question is how to I make sure that I am sending all this back as JSON in a get? As an added bonus to my problem, I don't know what fields will be sent to leasee.
我的问题是,如何确保将所有这些作为get中的JSON发送回来?作为对我问题的额外奖励,我不知道什么字段将被发送给leasee。
The POST looks like:
这个职位看起来像:
{"leasee":
{"profession":{"permanentMakeup":true,"esthetician":true,"hairStylist":true},
"compare":{"uniqueVsChic":"56"},
"name":"dfasdfasdf",
"contact":{"text":true,"facebook":true}}}
But GET gives me:
但给我:
{"leasee":
"{'compare': {'uniqueVsChic': '56'},
'profession': {'hairStylist':True,
'esthetician': True,
'permanentMakeup': True},
'name':'dfasdfasdf',
'contact':{'facebook': True,'text': True}}",
"created_at":"2015-05-22T23:27:13.598686Z",
"updated_at":"2015-05-22T23:27:13.609893Z"}
my views.py:
我的views.py:
from rest_framework import viewsets
from sitepages.models import SalonSubmission, StylistSubmission
from sitepages.serializers import SalonSerializer, StylistSerializer
from django.db.models.signals import post_save
from django.core.mail import send_mail
from django.dispatch import receiver
class SalonViewSet(viewsets.ModelViewSet):
queryset = SalonSubmission.objects.order_by('-created_at')
serializer_class = SalonSerializer
def perform_create(self, serializer):
instance = serializer.save()
post_save.send(sender=self.__class__, name=instance.business_name, phone=instance.phone)
return super(SalonViewSet, self).perform_create(serializer)
class StylistViewSet(viewsets.ModelViewSet):
queryset = StylistSubmission.objects.order_by('-created_at')
serializer_class = StylistSerializer
def perform_create(self, serializer):
instance = serializer.save()
post_save.send(sender=self.__class__, stylist=instance.leasee)
return super(StylistViewSet, self).perform_create(serializer)
@receiver(post_save, sender=SalonViewSet)
def send_email(sender, **kwargs):
send_mail(kwargs['name'], kwargs['phone'], 'from@example.com',
['to@example.com'], fail_silently=False)
@receiver(post_save, sender=StylistViewSet)
def send_email(sender, **kwargs):
send_mail('aName', kwargs['stylist'], 'from@example.com',
['to@example.com'], fail_silently=False)
My serializers.py:
我的serializers.py:
from rest_framework import serializers
from sitepages.models import SalonSubmission, StylistSubmission
class SalonSerializer(serializers.ModelSerializer):
class Meta:
model = SalonSubmission
fields = ('business_name', 'phone', 'created_at', 'updated_at')
read_only_fields = ('created_at', 'updated_at')
class StylistSerializer(serializers.ModelSerializer):
class Meta:
model = StylistSubmission
fields = (
'leasee',
'created_at',
'updated_at'
)
read_only_fields = ('created_at', 'updated_at')
My models.py:
我的models.py:
from django.db import models
django。数据库导入模型
class SalonSubmission(models.Model):
business_name = models.CharField(max_length=250)
phone = models.CharField(max_length=250)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class StylistSubmission(models.Model):
leasee = models.CharField(max_length=2000)
file = models.FileField(upload_to="/media/")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
1 个解决方案
#1
1
It appears that leasee = models.CharField(max_length=2000)
is a charfield. So Django is simply returning the string stored in this field. Also it is not storing valid JSON string neither because JSON only allows double quote.
看起来leasee = models.CharField(max_length=2000)是一个charfield。Django只是返回存储在这个字段中的字符串。它也不存储有效的JSON字符串,因为JSON只允许双引号。
{'compare': {'uniqueVsChic': '56'},
'profession': {'hairStylist':True,
'esthetician': True,
'permanentMakeup': True},
'name':'dfasdfasdf',
'contact':{'facebook': True,'text': True}}
looks like stringified dict
. So you can try to parse leasee
as a dict
in your serializer.
看起来像字符串化的命令。所以你可以尝试把leasee解析为序列化器中的命令。
#1
1
It appears that leasee = models.CharField(max_length=2000)
is a charfield. So Django is simply returning the string stored in this field. Also it is not storing valid JSON string neither because JSON only allows double quote.
看起来leasee = models.CharField(max_length=2000)是一个charfield。Django只是返回存储在这个字段中的字符串。它也不存储有效的JSON字符串,因为JSON只允许双引号。
{'compare': {'uniqueVsChic': '56'},
'profession': {'hairStylist':True,
'esthetician': True,
'permanentMakeup': True},
'name':'dfasdfasdf',
'contact':{'facebook': True,'text': True}}
looks like stringified dict
. So you can try to parse leasee
as a dict
in your serializer.
看起来像字符串化的命令。所以你可以尝试把leasee解析为序列化器中的命令。