2012-09-12 25 views
-2

我不知道爲什麼我得到錯誤。這裏有意見和clesses使用q.save()的IntegrityError:可能不是NULL

from polls.models import Word, Results 

def detail(request): 
    q = Word(type="How do you like") 
    q.save() 
    Word.objects.get(pk=1) 

    q.score_set.create(score=5) 
    q.score_set.create(score=4) 
    q.score_set.create(score=3) 
    q.score_set.create(score=2) 
    q.score_set.create(score=1) 


    return render_to_response('/$') 

Models.py

from django.db import models 


class Word(models.Model): 
    type = models.CharField(max_length=200) 

    def __unicode__(self): 
      return self.type 

class Results(models.Model): 
    word = models.ForeignKey(Word) 
    score = models.IntegerField() 

    def __unicode__(self): 
      return self.score 

錯誤:

IntegrityError at/
polls_word.score may not be NULLRequest Method: GET 
Request URL: Django Version: 1.4.1 
Exception Type: IntegrityError 
Exception Value: polls_word.score may not be NULL 
Exception Location: /home/oveledar/.virtualenvs/django/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py in execute, line 337 
Python Executable: /home/oveledar/.virtualenvs/django/bin/python 
Python Version: 2.6.6 
Python Path: ['/home/oveledar/django/mysite', 
'/home/oveledar/.virtualenvs/django/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg', 
'/home/oveledar/.virtualenvs/django/lib/python2.6/site-packages/pip-1.0.2-py2.6.egg', 
'/home/oveledar/.virtualenvs/django/lib64/python26.zip', 
'/home/oveledar/.virtualenvs/django/lib64/python2.6', 
'/home/oveledar/.virtualenvs/django/lib64/python2.6/plat-linux2', 
'/home/oveledar/.virtualenvs/django/lib64/python2.6/lib-tk', 
'/home/oveledar/.virtualenvs/django/lib64/python2.6/lib-old', 
'/home/oveledar/.virtualenvs/django/lib64/python2.6/lib-dynload', 
'/usr/lib/python2.6', 
'/usr/lib64/python2.6', 
'/home/oveledar/.virtualenvs/django/lib/python2.6/site-packages'] 
+1

請發表您的實際代碼。使用這裏的模型,「Word」沒有'score_set' - 它有'results_set'。 –

+0

不確定您的實際代碼是什麼意思。我有模型,視圖,網址和模板。我沒有利用網址和模板,所以我只發佈了模型和視圖。 – ono

+0

問題是,這段代碼會給你顯示的錯誤提供一個不同的錯誤 - 它會爲'score_set'提供一個'AttributeError'。 –

回答

1

你的表結構不正確,你需要:

class Results(models.Model): 
    word = models.ForeignKey(Word) 
    score = models.IntegerField(blank=True, null=True) 

後,取出此表並運行命令:

python manange.py syncdb 

blank選擇它了必需的或不是此節點的形式驗證

null爲你的數據庫選擇它`(空true或false)

+0

不幸的是,仍然是同樣的問題。 – ono

相關問題