Django saving json value to database/model

前端 未结 5 748
清歌不尽
清歌不尽 2020-12-28 19:30

Im new to django and im trying to save json to database. The problem is that im able to get data the data in my views but not sure how to save it in

5条回答
  •  滥情空心
    2020-12-28 20:24

    If I understand your question clearly then Your view should be something like.

    def add_comments(request):
        if 'application/x-www-form-urlencoded' in request.META['CONTENT_TYPE']:
            print 'hi'
            data = json.loads(request.body)
            comment = data.get('comment', None)
            id = data.get('id', None)
            title = data.get('title', None) 
    
            post = Post.objects.get(id = id)
            com = Comment()
            com. comments = comment
            com.title = post
            com.save()
    

提交回复
热议问题