Django JSON格式错误 [英] Django JSON format error

查看:125
本文介绍了Django JSON格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在POST请求中手动输入数据时遇到此错误.但是,当我在管理页面中执行此操作时,没有任何错误. "contact_number" 字段错误..

I am getting this error when I manually entered the data into POST request. But when I do it in the admin page, there is no error. Error at "contact_number" field..

在添加管理员时,这是正确的格式:

This is the correct format when adding in admin:

{
"name": "Santos",
"email": "san",
"address": "san",
"note": "san",
"contact_number": [
    "123455",
    "1231231",
    "23123123"
]

}

"contact_number" 字段中的错误. 这是在POST请求中通过邮递员添加时的结果:

Error at "contact_number" field. This is the result when adding via postman in POST request:

{
    "name": "3123",
    "email": "qwe@gmail.com",
    "address": "Col",
    "note": "noting",
    "contact_number": [
        "['3123', '123123']"
    ]
}

views.py

@method_decorator(csrf_exempt)
def phonebook_list(request):

    if request.method == 'GET':
        phonebooklist = PhoneBook.objects.all()
        serialized_data = [pb.to_json() for pb in phonebooklist]
        return JsonResponse(serialized_data, safe=False)
    elif request.method == 'POST':
            data= request.body.decode('utf8')
            data= json.loads(data)
            try:
                new_contact = PhoneBook.objects.create(name=data["name"],address=data["address"],email=data["email"],note=data["note"])
                ContactNumber.objects.create(contact_number=data["contact_number"],number=new_contact)
                serialized_data = [new_contact.to_json()]
                return JsonResponse({"created": serialized_data}, safe=False)
            except:
                return JsonResponse({"error":"not valid data"}, safe=False)

models.py

class PhoneBook(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=100, default='address')
    email = models.CharField(max_length=50, default='email')
    note = models.CharField(max_length=100, default='note')

    def to_json(self):

        contact_numbers = [c.contact_number
                     for c in self.contact_numbers.all()]
        return { 
            'name':            self.name, 
            'email':           self.email, 
            'address':         self.address,
            'note':            self.note,
            'contact_number': contact_numbers
         }

    def __str__(self):  
        return self.name

class ContactNumber(models.Model):  
    number = models.ForeignKey(PhoneBook, related_name="contact_numbers")
    contact_number= models.CharField(max_length=30)

    def __str__(self):  
        return self.contact_number

推荐答案

现在可以了,如果contactnumber大于1,我循环联系电话.

It's ok now, I looped the contact numbers if contactnumber is greater than 1.

  contact_number = data['contact_number']
  for x in contact_number:
    qs = ContactNumber(qs.number_id = new_contact.id, qs.contact_number = x)
    qs.save()

这篇关于Django JSON格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆