请原谅我英语不好。我是Django框架的新手。我想导出一个名为
title
的外键值,它在"产品"模型中。不同的django应用程序型号之间存在多种Foreignkey关系。请看下面所有这些型号:
产品/型号.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)
推车/型号.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']
我的工作模型是analytic/models.py,我想从这里导出XL表中的产品title
。
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)
最后分析/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
文档,了解导出时的高级数据操作。单击此处。