I have a Django app in which I maintain products for a webshop. Since many products have multiple variations and working in Excel is not optimal, I thought Python could help me and make the work easier. However, for the import into my store system I need a CSV file and here comes my problem.
I have a model with five fields where two are multiple fields (django-multiselectfield).
SIZES = (('16', 'Size 16'),
('17', 'Size 17'),
('18', 'Size 18'),
('19', 'Size 19'),
('20', 'Size 20'))
COLORS = (('white', 'White'),
('black', 'Black'),
('blue', 'Blue'),
('red', 'Red'),
('yellow', 'Yellow'))
class Items(models.Model):
name = models.CharField(_('Name'), max_length=254, null=True, blank=True, default='',)
sku = models.CharField(_('SKU'), max_length=254, null=True, blank=True, default='',)
size = MultiSelectField(_('Size'), choices=SIZES)
color = MultiSelectField(_('Color'), choices=COLORS)
price = models.DecimalField(_('Price'), max_digits=7, decimal_places=2)
I created two products to demonstrate my needs
In my CSV file, for example, I need a new line for each size and color so that the variation is created correctly in the webshop software.
So my CSV would have to look like this:
"type","sku","attribute 1 name","attribute 1 value","attribute 2 name","attribute 2 value"
"variable","abc01921","size","16","color","white,black,blue"
"variation","abc01921_white_16","size","16","color","white"
"variation","abc01921_black_16","size","16","color","black"
"variation","abc01921_blue_16","size","16","color","blue"
"variable","abc01831","size","16,17","color","black,blue"
"variation","abc01831_black_16","size","16","color","black"
"variation","abc01831_blue_16","size","16","color","blue"
"variation","abc01831_black_17","size","17","color","black"
"variation","abc01831_blue_17","size","17","color","blue"
How do I best implement this successfully? How can I check if it is a variable or a variation and use this information to write the CSV line into the file accordingly?

As I understand correctly you have to generate CSV file from Django database values?
To be able reproduce code without real Django model I created
Itemthat has same fields as your model. CSV generation code should work for Django real model objects as is (mostly... I'm not sure how model returnMultiSelectFieldvalues).Create list of example item objects:
Write csv file with all items:
Check csv resulted file:
If users of you Django app don't have access to Django server storage you should make CSV file downloadable via browser instead of save it to disk. @DarkDrifterX99 already answered how to implement CSV response