2017-04-06 112 views
0

,同时为在API中问题的答案(见下面的代码),我得到了以下错误:类型错误:模型构造函数没有位置参数

TypeError: Model constructor takes no positional arguments

有人能告诉我怎么解决这个问题?我使用的NDB模式

import webapp2 
import json 
from google.appengine.ext import ndb 

class AnswerExchange(ndb.Model): 
    answer=ndb.StringProperty(indexed=False,default="No Message") 

class AnswerHandler(webapp2.RequestHandler):  
    def create_answer(self,question_id): 
     try: 
      query = StackExchange.query(StackExchange.questionId == question_id)   
      questions = query.fetch(1) 
      ans_json = json.loads(self.request.body) 

      answerObj = AnswerExchange(answer=ans_json["answer"])     
      answerObj.put() 
      questions.answerName=answerObj 

      questions.put()     
     except: 
      raise webapp2.exc.HTTPBadRequest() 

class StackExchange(ndb.Model): 
    questionId=ndb.StringProperty(indexed=True) 
    questionName=ndb.StringProperty(indexed=False) 
    #answerID=ndb.StringProperty(indexed=True) 
    answerName=ndb.StructuredProperty(AnswerExchange,repeated=True) 


class StackHandler(webapp2.RequestHandler): 
    def get_questions(self): 
     #p = self.request.get('p') #get QueryString Parameter 
     #self.response.write(p) 
     query = StackExchange.query() 
     questions = query.fetch(6) 
     self.response.content_type = 'application/json' 
     self.response.write(json.dumps([d.to_dict() for d in questions])) 

    def get_question(self, question_id): 
     query = StackExchange.query(StackExchange.questionId == question_id)   
     questions = query.fetch(1) 
     self.response.content_type = 'application/json' 
     self.response.write(json.dumps([d.to_dict() for d in questions])) 

    def create_question(self): 
     try: 
      question_json = json.loads(self.request.body) 
      # if id in dept_json: 
      questionNo = question_json["questionId"] 
      questionToUpdate = StackExchange.query(StackExchange.questionId == questionNo); 
      #print deptToUpdate.count() 
      if questionToUpdate.count() == 0:    
       question = StackExchange(questionId=question_json["questionId"], questionName=question_json["questionName"])     
      else: 
       question = questionToUpdate.get() 
       question.questionName = question_json["questionName"] 

      #key = dept.put() 
      question.put()     
     except: 
      raise webapp2.exc.HTTPBadRequest() 

     self.response.headers['Location'] = webapp2.uri_for('get_question', question_id=questionNo) 
     self.response.status_int = 201 
     self.response.content_type = 'application/json' 
     #self.response.write(json.dumps()) 

    def create_answer(self,question_id): 
     #try: 
      question_json = json.loads(self.request.body) 
      questionNo = question_json["questionId"] 
      query = StackExchange.query(StackExchange.questionId == questionNo)   
      questions = query.fetch(1) 
      ans_json = json.loads(self.request.body) 

      answerObj = AnswerExchange(ans_json["answer"])     

      #answerObj.put() 
      #self.response.write(ans_json["answerName"]) 
      questions.answerName = answerObj 
      questions.put();    
     #except: 
     # raise webapp2.exc.HTTPBadRequest() 


    def get_answer(self, question_id): 
     query = StackExchange.query(StackExchange.questionId == question_id)   
     questions = query.fetch(1) 
     self.response.content_type = 'application/json' 
     self.response.write(json.dumps([d.to_dict() for d in questions])) 

app = webapp2.WSGIApplication([ 
    webapp2.Route(r'/api/v1/questions/<question_id>', methods=['GET'], handler='api.StackHandler:get_question', name='get_question'), 
    webapp2.Route(r'/api/v1/questions', methods=['GET'], handler='api.StackHandler:get_questions', name='get_questions'), 
    webapp2.Route(r'/api/v1/questions', methods=['POST'], handler='api.StackHandler:create_question', name='create_question'), 
    webapp2.Route(r'/api/v1/questions/<question_id>/answers', methods=['POST'], handler='api.StackHandler:create_answer', name='create_answer'), 
    webapp2.Route(r'/api/v1/questions/<question_id>/answers', methods=['GET'], handler='api.StackHandler:get_answer', name='get_answer') 
], debug=True) 
+0

请过贴上整个回溯 – plumSemPy

回答

1

变化,

answerObj = AnswerExchange(ans_json["answer"]) 

create_answer方法StackHandler

answerObj = AnswerExchange(answer=ans_json["answer"]) 
相关问题