Object does not support item assignment error

后端 未结 2 388
难免孤独
难免孤独 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:22

    Another way would be adding __getitem__, __setitem__ function

    def __getitem__(self, key):
        return getattr(self, key)
    

    You can use self[key] to access now.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题