내보기에서 내 models.py에 정의 된 메서드를 호출하려고하는데이 메시지가 표시됩니다.
unbound method add_question_history() must be called with QuestionHistory instance as first argument (got User instance instead)
이것은 내 모델입니다.
class QuestionHistory(models.Model):
question = models.ForeignKey(Question, blank=True, null=True)
user = models.OneToOneField('auth.User')
created_at = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return unicode(self.question)
def add_question_history(user, question):
qs = self.create(
question = question,
user = user
)
self.save()
그리고 이것은 뷰 에서이 메서드를 호출하는 방법입니다.
QuestionHistory.add_question_history(request.user, question_ID)
당신이하려는 것은 인스턴스 없이 인스턴스 메소드 를 호출하는 것 입니다.
당신이 원하는 것이 수업 방법 인 것 같습니다.
따라서 코드가 작동하려면 먼저 인스턴스를 만들거나 클래스 메서드add_question_history
로 만들어야 합니다 .
인스턴스 만들기 :
class QuestionHistory(models.Model):
question = models.ForeignKey(Question, blank=True, null=True)
user = models.OneToOneField('auth.User')
created_at = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return unicode(self.question)
def add_question_history(self, user, question):
qs = self.objects.create(
question = question,
user = user
)
qs.save()
#more stuff
QuestionHistory().add_question_history(request.user, questionID)
수업 방법 만들기 :
class QuestionHistory(models.Model):
question = models.ForeignKey(Question, blank=True, null=True)
user = models.OneToOneField('auth.User')
created_at = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return unicode(self.question)
@classmethod
def add_question_history(cls, user, question):
qs = cls.objects.create(
question = question,
user = user
)
qs.save()
#stuff
QuestionHistory.add_question_history(user,question)
더 나은 이해를 위해 이것을 읽으십시오
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다