0

I have an intractable problem driving me round the bend (arg!) I suspect the problem is that my model uses a choices argument for one field - am I assigning to it incorrectly?

model:

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)

call to it in views:

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'] is equal to a string of either 'C','W', or 'R'.

The error I receive is:

  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
talkingtoaj
  • 848
  • 8
  • 27
  • You shouldn't normally have to access `request.POST` directly. Have a look at model forms. – Alasdair Aug 31 '17 at 09:44
  • forms is my big weakness. When I use bootstrap and want to tweak the form field presentation directly I've found working with form objects a headache. I have to put some more time in learning to use them properly. – talkingtoaj Aug 31 '17 at 09:50

1 Answers1

1

In your view, you have spelled assessment correctly, but you have missed out an s in your model. Therefore you get the invalid keyword argument error.

If you rename the model field, you'll have to make a new migration and run it to update the database.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • You wouldn't know the answer to this question would you? https://stackoverflow.com/questions/45919248/whats-the-difference-between-custom-model-manager-methods-and-queryset-methods – talkingtoaj Aug 31 '17 at 09:56