/ admin / questions / useranswer /中的AttributeError

时间:2013-11-11 18:41:24

标签: python django admin attributeerror

我是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

models.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

views.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))

admin.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)   

3 个答案:

答案 0 :(得分: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''

答案 1 :(得分:1)

__unicode__()模型的UserAnswer方法存在问题。 answer可以为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)

答案 2 :(得分:0)

你不能有这样的结构:

def __unicode__(self, ):
  return self.answer.answer
在自我之后,有些东西丢失了。

不应该是这样的:

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