My goal is to take the xml response and save it to my database. I was able to access the first element with Elementtree which was products
The xml looks like this,
<?xml version="1.0" encoding="UTF-8"?> <products> <item id="0"> <product_id> ... <product_name> ... <product_url> ... <advertiser> ... <designer> ... <image_url> ... <price> ... <commission> ... </item> When I try and iterate over it I get end printed out for each element.
def advertisers(request): url = 'https://api.example.com/apikey' response = requests.get(url, stream=True) response.raw.decode_content = True items = ElementTree.iterparse(response.raw) for elem, event in items: print(elem) return HttpResponse(elem) Output end end end end end end end If my model looks like this, would someone please show me how to structure this loop to save the elements to the database. I know if I loop through them and append each set I can call items.save() and all should be well. I just want to make sure I am accessing the correctly first.
class Products(models.Model): product_id = models.CharField(max_length=100) product_name = models.CharField(max_length=100) product_url = models.CharField(max_length=100) advertiser = models.CharField(max_length=100) designer = models.CharField(max_length=100) image_url = models.CharField(max_length=100) price = models.CharField(max_length=100) commission = models.CharField(max_length=100) def __str__(self): return self.products
No comments:
Post a Comment