Python-Redmine中setattr()的使用查询

Query about usage of setattr() in Python-Redmine

setattr() 对于 Redmine 问题中的项目,失败,出现以下错误。

Traceback (most recent call last):
  File "E:\test\get_redmine_data.py", line 47, in <module>
    print (item.assigned_to)
  File "C:\Python27\lib\site-packages\redminelib\resources\standard.py", line 150, in __getattr__
    return super(Issue, self).__getattr__(attr)
  File "C:\Python27\lib\site-packages\redminelib\resources\base.py", line 164, in __getattr__
    attr, encoded = self.encode(attr, decoded, self.manager)
  File "C:\Python27\lib\site-packages\redminelib\resources\base.py", line 266, in encode
    return attr, manager.new_manager(cls._resource_map[attr]).to_resource(value)
  File "C:\Python27\lib\site-packages\redminelib\managers\base.py", line 29, in to_resource
    return self.resource_class(self, resource)
  File "C:\Python27\lib\site-packages\redminelib\resources\base.py", line 130, in __init__
    self._decoded_attrs = dict(dict.fromkeys(relations_includes), **attributes)
TypeError: type object argument after ** must be a mapping, not str

我正在尝试设置一些默认受让人,以解决未设置受让人的问题。代码在打印我刚刚设置的属性的行失败。我的代码如下:

redmine = Redmine('http://redmine_url', username='uname', password='pwd')
project = redmine.project.get('proj_name')
work_items = project.issues
for item in work_items:
   assignee_not_set = getattr(item,'assigned_to',True)
   if assignee_not_set == True:
       print item.id
       setattr(item,'assigned_to','Deepak')
       print (item.assigned_to)

我也试过使用 update() 方法,

redmine.project.update(item.id, assigned_to='Deepak')

这也因另一个错误而失败 - redminelib.exceptions.ResourceNotFoundError: Requested resource doesn't exist。 我验证了 Redmine 中存在问题 ID。

你这里有几个问题:

  1. 属性名称是 assigned_to_id 而不是 assigned_to
  2. 它接受 int 的用户 ID 而不是 str
  3. 的用户名
  4. 这里不用setattr(),直接用item.assigned_to_id = 123
  5. 设置assigned_to_id后需要调用item.save()否则不会保存到Redmine
  6. 当您尝试使用 update() 方法时,您使用的是 Project 资源而不是 Issue 资源,这就是为什么您得到 ResourceNotFoundError

文档中提供了所有这些信息:https://python-redmine.com/resources/issue.html