如何将我的 Django 模型导出到自定义 csv 中?
How do I export my django model into custom csv?
我有一个 Django 应用程序,我在其中维护网上商店的产品。由于许多产品有多种变体并且在 Excel 中工作并不是最佳选择,我认为 Python 可以帮助我并使工作更轻松。但是,为了导入我的商店系统,我需要一个 CSV 文件,我的问题来了。
我有一个包含五个字段的模型,其中两个是多个字段 (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)
我创建了两个产品来展示我的需求
例如,在我的 CSV 文件中,我需要为每种尺寸和颜色换行,以便在网上商店软件中正确创建变体。
因此我的 CSV 必须如下所示:
"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"
我怎样才能最好地成功实施它?我如何检查它是变量还是变体,并使用此信息相应地将 CSV 行写入文件?
I have a model with five fields where two are multiple fields (django-multiselectfield)
I need a new line for each size and color so that the variation is created correctly in the webshop software.
我假设每个产品数据库条目都是一个“变量”,然后在 writing to the CSV for each count of the two multiselect fields.
时手动创建“变体”
据我了解,您必须从 Django 数据库值生成 CSV 文件吗?
为了能够在没有真实 Django 模型的情况下重现代码,我创建了 Item
,它具有与您的模型相同的字段。 CSV 生成代码应该按原样适用于 Django 真实模型对象(主要是......我不确定模型 return MultiSelectField
值如何)。
创建示例项目对象列表:
class Item:
def __init__(self, **kw):
self.name = kw['name']
self.sku = kw['sku']
self.size = kw['size']
self.color = kw['color']
self.price = kw['price']
items = [
Item(
**{
'name': 'product1',
'sku': 'abc123',
'size': ('16', '17'),
'color': ('green', 'orange'),
'price': 123
}
),
Item(
**{
'name': 'product2',
'sku': 'abc321',
'size': ('61', '71'),
'color': ('yellow', 'red'),
'price': 321
}
),
]
写入包含所有项目的 csv 文件:
header = ["type", "sku", "attribute 1 name", "attribute 1 value", "attribute 2 name", "attribute 2 value"]
csv_lines = [header]
for item in items:
csv_lines.append(['variable', item.sku, 'size', ','.join(item.size), 'color', ','.join(item.color)])
for size_variation in item.size:
for color_variation in item.color:
csv_lines.append(['variation', f'{item.sku}_{color_variation}_{size_variation}', 'size', size_variation, 'color', color_variation])
import csv
with open('./csv_file_to_import.csv', 'w', newline='') as csv_file:
write = csv.writer(csv_file, quoting=csv.QUOTE_ALL)
write.writerows(csv_lines)
检查 csv 结果文件:
"type","sku","attribute 1 name","attribute 1 value","attribute 2 name","attribute 2 value"
"variable","abc123","size","16,17","color","green,orange"
"variation","abc123_green_16","size","16","color","green"
"variation","abc123_orange_16","size","16","color","orange"
"variation","abc123_green_17","size","17","color","green"
"variation","abc123_orange_17","size","17","color","orange"
"variable","abc321","size","61,71","color","yellow,red"
"variation","abc321_yellow_61","size","61","color","yellow"
"variation","abc321_red_61","size","61","color","red"
"variation","abc321_yellow_71","size","71","color","yellow"
"variation","abc321_red_71","size","71","color","red"
如果您的 Django 应用程序的用户无法访问 Django 服务器存储,您应该使 CSV 文件可通过浏览器下载,而不是将其保存到磁盘。 @DarkDrifterX99 已经 如何实现 CSV 响应
我有一个 Django 应用程序,我在其中维护网上商店的产品。由于许多产品有多种变体并且在 Excel 中工作并不是最佳选择,我认为 Python 可以帮助我并使工作更轻松。但是,为了导入我的商店系统,我需要一个 CSV 文件,我的问题来了。
我有一个包含五个字段的模型,其中两个是多个字段 (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)
我创建了两个产品来展示我的需求
因此我的 CSV 必须如下所示:
"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"
我怎样才能最好地成功实施它?我如何检查它是变量还是变体,并使用此信息相应地将 CSV 行写入文件?
I have a model with five fields where two are multiple fields (django-multiselectfield)
I need a new line for each size and color so that the variation is created correctly in the webshop software.
我假设每个产品数据库条目都是一个“变量”,然后在 writing to the CSV for each count of the two multiselect fields.
时手动创建“变体”据我了解,您必须从 Django 数据库值生成 CSV 文件吗?
为了能够在没有真实 Django 模型的情况下重现代码,我创建了 Item
,它具有与您的模型相同的字段。 CSV 生成代码应该按原样适用于 Django 真实模型对象(主要是......我不确定模型 return MultiSelectField
值如何)。
创建示例项目对象列表:
class Item:
def __init__(self, **kw):
self.name = kw['name']
self.sku = kw['sku']
self.size = kw['size']
self.color = kw['color']
self.price = kw['price']
items = [
Item(
**{
'name': 'product1',
'sku': 'abc123',
'size': ('16', '17'),
'color': ('green', 'orange'),
'price': 123
}
),
Item(
**{
'name': 'product2',
'sku': 'abc321',
'size': ('61', '71'),
'color': ('yellow', 'red'),
'price': 321
}
),
]
写入包含所有项目的 csv 文件:
header = ["type", "sku", "attribute 1 name", "attribute 1 value", "attribute 2 name", "attribute 2 value"]
csv_lines = [header]
for item in items:
csv_lines.append(['variable', item.sku, 'size', ','.join(item.size), 'color', ','.join(item.color)])
for size_variation in item.size:
for color_variation in item.color:
csv_lines.append(['variation', f'{item.sku}_{color_variation}_{size_variation}', 'size', size_variation, 'color', color_variation])
import csv
with open('./csv_file_to_import.csv', 'w', newline='') as csv_file:
write = csv.writer(csv_file, quoting=csv.QUOTE_ALL)
write.writerows(csv_lines)
检查 csv 结果文件:
"type","sku","attribute 1 name","attribute 1 value","attribute 2 name","attribute 2 value"
"variable","abc123","size","16,17","color","green,orange"
"variation","abc123_green_16","size","16","color","green"
"variation","abc123_orange_16","size","16","color","orange"
"variation","abc123_green_17","size","17","color","green"
"variation","abc123_orange_17","size","17","color","orange"
"variable","abc321","size","61,71","color","yellow,red"
"variation","abc321_yellow_61","size","61","color","yellow"
"variation","abc321_red_61","size","61","color","red"
"variation","abc321_yellow_71","size","71","color","yellow"
"variation","abc321_red_71","size","71","color","red"
如果您的 Django 应用程序的用户无法访问 Django 服务器存储,您应该使 CSV 文件可通过浏览器下载,而不是将其保存到磁盘。 @DarkDrifterX99 已经