0

我是 Django 和 Python 的新手。当我单击管理页面中的用户答案时,我收到错误消息。我知道错误的位置是“返回 self.answer.answer”,但我不知道为什么或如何解决它。

错误

AttributeError at /admin/questions/useranswer/
'NoneType' object has no attribute 'answer'
Request Method: GET
Request URL:    hxxp://x.x.x.x:8000/admin/questions/useranswer/
Django Version: 1.5.1
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'answer'
Exception Location: /Users/xxx/Desktop/matchmaker/src/questions/models.py in unicode line   31 "which is return self.answer.answer"
Python Executable:  /Users/xxx/Desktop/matchmaker/bin/python
Python Version: 2.7.5

模型.py

from django.db import models
from django.contrib.auth.models import User


class Question(models.Model):
   user = models.ForeignKey(User)
   question = models.CharField(max_length=120)
   timestamp = models.DateField(auto_now_add=True, auto_now=False)
   update = models.DateField(auto_now_add=False, auto_now=True)

   def __unicode__(self, ):
      return self.question

class Answer(models.Model):
   question = models.ForeignKey(Question)
   answer = models.CharField(max_length=120)
   timestamp = models.DateField(auto_now_add=True, auto_now=False)
   update = models.DateField(auto_now_add=False, auto_now=True)

   def __unicode__(self, ):
      return self.answer

class UserAnswer(models.Model):
   user = models.ForeignKey(User)
   question = models.ForeignKey(Question)
   answer = models.ForeignKey(Answer, null=True, blank=True)
   timestamp = models.DateField(auto_now_add=True, auto_now=False)
   update = models.DateField(auto_now_add=False, auto_now=True)

   def __unicode__(self, ):
      return self.answer.answer

视图.py

from django.contrib.auth.models import User
from django.shortcuts import render_to_response, RequestContext, Http404,  HttpResponseRedirect


from .models import Question, Answer, UserAnswer


def all_questions(request):
   questions = Question.objects.all()

if request.method == 'POST':
    question_id = request.POST['question_id']
    answer_form = request.POST['answer']
    user = User.objects.get(username=request.user)
    question = Question.objects.get(id=question_id)
    answer = Answer.objects.get(question=question, answer=answer_form)
    answered, created = UserAnswer.objects.get_or_create(user=user, question=question)
    answered.answer = answer
    answered.save()

    return render_to_response('questions/all.html', locals(), context_instance=RequestContext(request))

管理员.py

from django.contrib import admin

from .models import Question, Answer, UserAnswer


class QuestionAdmin(admin.ModelAdmin):
  class Meta:
    model = Question

admin.site.register(Question, QuestionAdmin)


class AnswerAdmin(admin.ModelAdmin):
  class Meta:
    model = Answer

admin.site.register(Answer, AnswerAdmin)

class UserAnswerAdmin(admin.ModelAdmin):
  class Meta:
    model = UserAnswer

admin.site.register(UserAnswer, UserAnswerAdmin)   
4

3 回答 3

2

你的问题在这里:

answer = models.ForeignKey(Answer, null=True, blank=True)

由于它可以为空,并且您__unicode__的定义为

def __unicode__(self, ):
    return self.answer.answer

如果self.answerNone,则None.answer抛出错误

因此,将__unicode__方法更改为:

def __unicode__(self): #Remove the comma
    return u"%s" % self.answer.answer if self.answer else u''
于 2013-11-11T18:45:20.517 回答
1

__unicode__()模型方法有问题UserAnsweranswer可以为 null/none,因此您需要处理此类情况。

您可以对其进行更新以处理以下情况:

class UserAnswer(models.Model):
   user = models.ForeignKey(User)
   ...

   def __unicode__(self, ):
      if self.answer:
          return self.answer.answer
      else:
         return unicode(self.id)
于 2013-11-11T18:47:13.123 回答
0

你不能有这样的结构:

def __unicode__(self, ):
  return self.answer.answer

在 self 之后缺少一些东西。

不应该是这样的:

def __unicode__(self):
  return self.answer.answer
于 2013-11-11T18:45:50.983 回答