2017-08-31 26 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

相关问题