2017-08-31 53 views
0

我有一個棘手的問題促使我轉彎(arg!)我懷疑問題是我的模型對一個字段使用了選擇參數 - 我是否將其分配給它不正確?模型選擇 - 此功能無效的關鍵字參數

模型:

class Attempt(models.Model): 
    # User attempt and results at a question 
    # Records their result, points to an Entry related to what they typed, records the user, ELO and variance at time 


    CORRECT = 'C' 
    WRONG = 'W' 
    REPORT = 'R' 
    A_TYPE_CHOICES = ((CORRECT, 'Right'), (WRONG, 'Wrong'), (REPORT, 'There is a problem with the question')) 

    user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL) 
    entry = models.ForeignKey('Entry', on_delete=models.CASCADE) 
    assesment = models.CharField(max_length=1,choices=A_TYPE_CHOICES, db_index=True) 
    feedback = models.CharField(max_length=200, help_text="What is it about the question that makes it unclear or misleading",blank=True) 
    score = models.FloatField(null=True, blank=True, help_text="score of the user when this attempt was made - only recorded if variance below a certain threshold, otherwise null")  
    variance = models.FloatField() 
    created = models.DateTimeField(auto_now=True) 

調用它的觀點:

Attempt.objects.create(user=request.user, 
         entry=Entry.objects.get(id=request.POST['entry_id']), 
         assessment=request.POST['self_assessment'], 
         feedback=request.POST['feedback'], 
         score=sp.score, 
         variance=sp.variance, 
         ) 

request.POST [ 'self_assessment']是等於或者 'C' 的字符串, 'W',或'R'。

我收到的錯誤是:

File "C:\Users\Win7\OneDrive\Programming\Git\lang\Quiz\views\assessment.py", line 174, in question_score 
    variance=sp.variance, 
    File "C:\Users\Win7\OneDrive\Programming\VirtualEnvs\lang\lib\site-packages\django\db\models\manager.py", line 85, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
    File "C:\Users\Win7\OneDrive\Programming\VirtualEnvs\lang\lib\site-packages\django\db\models\query.py", line 392, in create 
    obj = self.model(**kwargs) 
    File "C:\Users\Win7\OneDrive\Programming\VirtualEnvs\lang\lib\site-packages\django\db\models\base.py", line 571, in __init__ 
    raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0]) 
TypeError: 'assessment' is an invalid keyword argument for this function 
+0

您通常不必直接訪問'request.POST'。看看模型表單。 – Alasdair

+0

表格是我的大缺點。當我使用引導程序並想直接調整表單字段表示時,我發現使用表單對象很頭疼。我必須花更多時間學習如何正確使用它們。 – talkingtoaj

回答

1

在你看來,你已經拼寫正確assessment,但你已經錯過了在模型的s。因此你會得到無效的關鍵字參數錯誤。

如果您重命名模型字段,則必須進行新的遷移並運行它來更新數據庫。

+0

你不會知道這個問題的答案嗎? https://stackoverflow.com/questions/45919248/whats-the-difference-between-custom-model-manager-methods-and-queryset-methods – talkingtoaj

相關問題