我通过POST将JSON发送到视图。我得到的回复只是部分JSON,其余的从双引号变为单引号。
我的问题是如何确保我将所有这些作为JSON发送回来?作为我的问题的额外奖励,我不知道将哪些字段发送给租赁者。
POST看起来像:
{"leasee":
{"profession":{"permanentMakeup":true,"esthetician":true,"hairStylist":true},
"compare":{"uniqueVsChic":"56"},
"name":"dfasdfasdf",
"contact":{"text":true,"facebook":true}}}
但是GET给了我:
{"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:
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)
我的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')
我的models.py:
来自django.db导入模型
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)
答案 0 :(得分:2)
似乎leasee = models.CharField(max_length=2000)
是一个字段。所以Django只是返回存储在该字段中的字符串。它也不存储有效的JSON字符串,因为JSON只允许双引号。
{'compare': {'uniqueVsChic': '56'},
'profession': {'hairStylist':True,
'esthetician': True,
'permanentMakeup': True},
'name':'dfasdfasdf',
'contact':{'facebook': True,'text': True}}
看起来像字符串dict
。
因此,您可以尝试在序列化程序中将leasee
解析为dict
。