Home > Net >  Django - Product Details in XML file
Django - Product Details in XML file

Time:12-20

I have a task to export all product details such as name, price etc. from db to XML file. Since now i'm exporting most of the fields and save them to an XML file. However i'm a bit confused on how to export images. I have 2 models one for Product and one for ProductImages, see below:

models.py

class Product(models.Model):
    category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
    name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=500, null=True, unique=True)
    sku = models.CharField(max_length=100, unique=True)
    image = models.ImageField(upload_to='photos/%Y/%m/%d/')
    created_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now=True)

class ProductImage(models.Model):
    product = models.ForeignKey(Product, related_name='images', on_delete=models.CASCADE)
    title = models.CharField(max_length=50, blank=True)
    image = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True, null=True)

Also according to requirements there are two fields where images should be exported. If there is one image (product table) should be exported to item_image_link which i exporting it with no problem. And if there are more than one (ProductImage table) to item_additional_image_link and here is where i have issue.

I iterate over products table like below and then trying to find all images for specific product id like:

products = Product.objects.filter(product_status=True)
images = ProductImage.objects.filter(product__id__in=products)

for products in products:
  item = ET.SubElement(channel, "item")
  g_item_id = ET.SubElement(item, ("{http://base.google.com/ns/1.0}id")).text = products.sku
  g_item_image_link = ET.SubElement(item, ("{http://base.google.com/ns/1.0}image_link")).text = 'http://127.0.0.1:8000' products.image.url
  for image in images:
    g_item_additional_image_link = ET.SubElement(item, ("{http://base.google.com/ns/1.0}additional_image_link")).text = 'http://127.0.0.1:8000' image.image.url

However the g_item_additional_image_link brings all images i have in db for all products. I suppose the problem is in the images query. How can i show images related to each product?

Thank you

CodePudding user response:

You don't need to define an extra query for the images as django links them for you. First, add a prefetch_related to the products query. That way django does not create a second query when ietrating over images:

products = Product.objects.filter(product_status=True).prefetch_related('images')

And then, just iterate over the related images:

for image in product.images.all():
...

You also should use product instead of products when iterating over them.

products = Product.objects.filter(product_status=True).prefetch_related('images')

for product in products:
    item = ET.SubElement(channel, "item")
    g_item_id = ET.SubElement(item, ("{http://base.google.com/ns/1.0}id")).text = product.sku
    g_item_image_link = ET.SubElement(item, ("{http://base.google.com/ns/1.0}image_link")).text = 'http://127.0.0.1:8000' products.image.url
    for image in product.images.all():
        g_item_additional_image_link = ET.SubElement(item, ("{http://base.google.com/ns/1.0}additional_image_link")).text = 'http://127.0.0.1:8000' image.image.url
  • Related