Object does not support item assignment error

后端 未结 2 471
难免孤独
难免孤独 2021-02-01 12:19

In my views.py I assign values before saving the form. I used to do it the following way:

projectForm.lat = session_results[\'lat\']
projectForm.lng         


        
2条回答
  •  不要未来只要你来
    2021-02-01 12:23

    The error seems clear: model objects do not support item assignment. MyModel.objects.latest('id')['foo'] = 'bar' will throw this same error.

    It's a little confusing that your model instance is called projectForm...

    To reproduce your first block of code in a loop, you need to use setattr

    for k,v in session_results.iteritems():
        setattr(projectForm, k, v)
    

提交回复
热议问题