this is my code. I am trying to translate xml string into python list to be shown in html template.
self.task_xml = "<?xml version="1.0" encoding="utf-8"?> <django-objects version="1.0"> <object model="task.task" pk="31"> <field name="name" type="CharField">New Task</field> <field name="parent_task_id" type="IntegerField">0</field> </object> <object model="task.task" pk="32"> <field name="name" type="CharField">New Task</field> <field name="parent_task_id" type="IntegerField">0</field> </object> <object model="task.task" pk="33"> <field name="name" type="CharField">New Task</field> <field name="parent_task_id" type="IntegerField">31</field> </object> <object model="task.task" pk="34"> <field name="name" type="CharField">New Task</field> <field name="parent_task_id" type="IntegerField">31</field> </object> </django-objects>" 58 self.xmlData = ET.fromstring(self.db.task_xml) 59 60 self.task_list = [] 61 taskList = [] 62 for obj in self.xmlData.iter("object"): 63 parent_task_id = obj.find("field[@name='parent_task_id']").text 64 if parent_task_id == EMPTY_UUID: 65 taskList.append({'id': obj.get("pk"), 66 'name': obj.find("field[@name='name']").text, 67 'parent_task_id': parent_task_id , 68 }) 69 # Apprend taskList: 70 for task in taskList: 71 taskViewModel = TaskViewModel(task.id, True) 72 self.task_list.append(taskViewModel)
but I am getting error:
'dict' object has no attribute 'id'
and it is task.id in line 71
do you think i have a problem with this in line 65:
'id': obj.get("pk"
The "AttributeError: 'dict' object has no attribute" means that we are trying to access an attribute or call a method on a dictionary that is not implemented by the dict class. What is this? If you are trying to add key-value pairs to a dictionary, use bracket notation.
The Python "AttributeError: 'dict' object has no attribute 'iteritems'" occurs because the iteritems() method has been removed in Python 3. To solve the error, use the items() method, e.g. my_dict. items() , to get a view of the dictionary's items.
The __dict__ in Python represents a dictionary or any mapping object that is used to store the attributes of the object. They are also known as mappingproxy objects. To put it simply, every object in Python has an attribute that is denoted by __dict__.
You are accessing the dictionary wrongly. You need to use subscript with string 'id'
, Example -
taskViewModel = TaskViewModel(task['id'], True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With