Mit Form i XML-Anforderung erstellen und xml reagieren erhalten, die wie folgt aussieht:Django String-Indizes müssen ganze Zahlen sein, str nicht
<Root>
<Header>
<information>info</information>
</Header>
<Main>
<Product>
<Name>name1</Name>
<Description>description1</Description>
<Price>1</Price>
<Pictures>
<Picture>url_1</Picture>
<Picture>url_2</Picture>
</Pictures>
</Product>
</Main>
</Root>
Dann diese Funktion mit i XML-Daten Wörterbuch konvertieren:
from xml.etree import cElementTree as ET
from collections import defaultdict
def etree_to_dict(t):
d = {t.tag: {} if t.attrib else None}
children = list(t)
if children:
dd = defaultdict(list)
for dc in map(etree_to_dict, children):
for k, v in dc.iteritems():
dd[k].append(v)
d = {t.tag: {k:v[0] if len(v) == 1 else v for k, v in dd.iteritems()}}
if t.attrib:
d[t.tag].update(('@' + k, v) for k, v in t.attrib.iteritems())
if t.text:
text = t.text.strip()
if children or t.attrib:
if text:
d[t.tag]['#text'] = text
else:
d[t.tag] = text
return d
e = ET.XML('''
<Root>
<Header>
<information>info</information>
</Header>
<Main>
<Product>
<Name>name1</Name>
<Description>description1</Description>
<Price>1</Price>
<Pictures>
<Picture>url_1</Picture>
<Picture>url_2</Picture>
</Pictures>
</Product>
</Main>
</Root>
''')
und speichern sie zur Datenbank:
from pprint import pprint
d = etree_to_dict(e)
pprint(d)
d = etree_to_dict(e)
product = d['Root']['Main']['Product']
r = Product.objects.create()
r.name = product['Name']
r.description = product['Description']
r.price = product['Price']
r.save()
Und alles geklappt hat. Aber wenn ich versuche Bilder zu Datenbank zu speichern:
product_pictures=d['Root']['Main']['Pictures']
for m in product_pictures:
p = ProductPictures(
picture = m['Picture']
)
p.product = r
p.save()
r.productpictures_set.all()
Ich habe TypeError
string indices must be integers, not str
picture = m['Picture']
auf Zeichenfolge bekam. Warum passiert das? Was ich falsch mache. Vielen Dank für Ihre Antwort.
Hier ist mein Modell:
class Product(models.Model):
name = models.CharField(max_length=200, blank=True, null=True)
description = models.TextField(max_length=2000, blank=True, null=True)
price = models.CharField(max_length=10, blank=True, null=True)
class ProductPictures(models.Model):
product = models.ForeignKey(Product, null=True)
picture = models.CharField(max_length=200, blank=True, null=True)
UPD: hier ist, Daten von Local vars
:
product_pictures
{'Picture': [{'#text': 'http://images.static.goglobal.travel/images/hotels/67862/2010113161131438.jpg',
'@Description': ''},
{'#text': 'http://images.static.goglobal.travel/images/hotels/67862/2010113161132187.jpg',
'@Description': ''},
{'#text': 'http://images.static.goglobal.travel/images/hotels/67862/2010113161132406.jpg',
'@Description': ''}]}
m
'Picture'
d
{'Root': {'Header': {'Information': '1521337'},
'Main': {'Name': 'name1',
'Price': '1',
'Description': 'description',
'Pictures': {'Picture': [{'#text': 'http://images.static.goglobal.travel/images/hotels/67862/2010113161131438.jpg',
'@Description': ''},
{'#text': 'http://images.static.goglobal.travel/images/hotels/67862/2010113161132187.jpg',
'@Description': ''},
{'#text': 'http://images.static.goglobal.travel/images/hotels/67862/2010113161132406.jpg',
'@Description': ''}]},
'RoomFacilities': None}}}
'm' scheint eine Zeichenfolge zu sein, kein Wörterbuch. Obwohl ich nicht herausfinden kann, wo in Ihrem Code Sie eine Zeichenfolge machen. – Sayse
der volle Fehler Stack bitte – e4c5
Können Sie den Inhalt des "d" Wörterbuchs ausdrucken? – raphv