django-import-export 包在导出到 XL 文件时获取外键值
django-import-export package get a Foreignkey value while Export to XL file
首先请原谅我的英语不好。我是 Django Framework 的新手。我想导出一个名为 title
的外键值,它位于 'Product' 模型中。不同的 django 应用程序模型之间存在多个外键关系。请在下面查看所有这些模型:
products/models.py
class Product(models.Model):
title = models.CharField(max_length=500)
brand = models.ForeignKey(
Brand, on_delete=models.CASCADE, null=True, blank=True)
image = models.ImageField(upload_to='products/', null=True, blank=True)
price = models.DecimalField(decimal_places=2, max_digits=20, default=450)
old_price = models.DecimalField(
default=1000, max_digits=20, decimal_places=2)
weight = models.CharField(
max_length=20, help_text='20ml/20gm', null=True, blank=True)
size = models.CharField(
max_length=10, help_text='S/M/L/XL/32/34/36', null=True, blank=True)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
carts/models.py
class Entry(models.Model):
product = models.ForeignKey(Product, null=True, on_delete=models.CASCADE)
cart = models.ForeignKey(Cart, null=True, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
price = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
last_purchase_price = models.DecimalField(
default=0.0, max_digits=20, decimal_places=15)
weight_avg_cost = models.DecimalField(
default=0.0, max_digits=20, decimal_places=15)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-timestamp']
我的工作模型是 analytics/models.py,我想从那里导出产品 title
XL sheet.
class AllReportModel(models.Model):
# All report model
invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, null=True, blank=True)
ws_invoice = models.ForeignKey(WholesaleInvoice, on_delete=models.CASCADE, null=True, blank=True)
entry = models.ForeignKey(Entry, on_delete=models.CASCADE, null=True, blank=True)
stock_quantity = models.IntegerField()
timestamp = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
最后 analytics/admin.py 是:
class AllReportModelResource(resources.ModelResource):
class Meta:
model = AllReportModel
fields = ('id', 'invoice', 'ws_invoice', 'entry', 'stock_quantity')
class AllReportModelAdmin(ImportExportModelAdmin):
resource_class = AllReportModelResource
list_display = ['invoice', 'ws_invoice', 'entry', 'timestamp']
search_fields = ['invoice']
admin.site.register(AllReportModel, AllReportModelAdmin)
将不胜感激。
我遇到过这种情况,解决起来很简单。
您可以参考 django-import-export
文档了解导出时的高级数据操作 Click HERE。
首先请原谅我的英语不好。我是 Django Framework 的新手。我想导出一个名为 title
的外键值,它位于 'Product' 模型中。不同的 django 应用程序模型之间存在多个外键关系。请在下面查看所有这些模型:
products/models.py
class Product(models.Model):
title = models.CharField(max_length=500)
brand = models.ForeignKey(
Brand, on_delete=models.CASCADE, null=True, blank=True)
image = models.ImageField(upload_to='products/', null=True, blank=True)
price = models.DecimalField(decimal_places=2, max_digits=20, default=450)
old_price = models.DecimalField(
default=1000, max_digits=20, decimal_places=2)
weight = models.CharField(
max_length=20, help_text='20ml/20gm', null=True, blank=True)
size = models.CharField(
max_length=10, help_text='S/M/L/XL/32/34/36', null=True, blank=True)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
carts/models.py
class Entry(models.Model):
product = models.ForeignKey(Product, null=True, on_delete=models.CASCADE)
cart = models.ForeignKey(Cart, null=True, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
price = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
last_purchase_price = models.DecimalField(
default=0.0, max_digits=20, decimal_places=15)
weight_avg_cost = models.DecimalField(
default=0.0, max_digits=20, decimal_places=15)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-timestamp']
我的工作模型是 analytics/models.py,我想从那里导出产品 title
XL sheet.
class AllReportModel(models.Model):
# All report model
invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, null=True, blank=True)
ws_invoice = models.ForeignKey(WholesaleInvoice, on_delete=models.CASCADE, null=True, blank=True)
entry = models.ForeignKey(Entry, on_delete=models.CASCADE, null=True, blank=True)
stock_quantity = models.IntegerField()
timestamp = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
最后 analytics/admin.py 是:
class AllReportModelResource(resources.ModelResource):
class Meta:
model = AllReportModel
fields = ('id', 'invoice', 'ws_invoice', 'entry', 'stock_quantity')
class AllReportModelAdmin(ImportExportModelAdmin):
resource_class = AllReportModelResource
list_display = ['invoice', 'ws_invoice', 'entry', 'timestamp']
search_fields = ['invoice']
admin.site.register(AllReportModel, AllReportModelAdmin)
将不胜感激。
我遇到过这种情况,解决起来很简单。
您可以参考 django-import-export
文档了解导出时的高级数据操作 Click HERE。