在Python-Redmine中查询setattr()的用法

时间:2019-02-13 11:46:46

标签: python redmine 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。 我确认问题ID在Redmine中存在。

1 个答案:

答案 0 :(得分:2)

您在这里遇到几个问题:

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

所有这些信息都可以在文档中找到:https://python-redmine.com/resources/issue.html