模型选择 - 此函数的

时间:2017-08-31 09:38:27

标签: django django-models django-views

我有一个棘手的问题让我绕弯道(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

1 个答案:

答案 0 :(得分:1)

在您看来,您已正确拼写assessment,但您错过了模型中的s。因此,您将获得无效的关键字参数错误。

如果重命名模型字段,则必须进行新的迁移并运行它以更新数据库。