鉴于这些模型:
class Company(models.Model):
company_name = models.CharField(max_length=50, unique=True)
....
class Product(models.Model):
company = models.ForeignKey(Company)
product_name = models.CharField(max_length=100)
...
class Inventory(models.Model):
product = models.ForeignKey(Product, null=True, unique=True)
...
从XLS导入到库存,并正确指定了company_name和product_name,即XLS文件包含一行,用于指定唯一产品的company_name和product_name。
产品对象可以在Django / python中找到::
Product.objects.filter(company__company_name=company_name, product_name=product_name)
如何构建Django-import-export resources.ModelResources以支持通过admin导入?
答案 0 :(得分:-2)
在您的Admin.py中使用此代码。
from import_export.admin import ImportExportModelAdmin,ExportMixin
from import_export import fields,widgets
from import_export import resources
from django.contrib.admin import DateFieldListFilter
class ProductResource(resources.ModelResource):
def export(self, queryset=None):
if queryset is None:
queryset = self.get_queryset()
headers = self.get_export_headers()
data = tablib.Dataset(headers=headers)
for obj in queryset.iterator():
data.append(self.export_resource(obj))
return data
class Meta:
model = Product
class ProductAdmin(ImportExportModelAdmin):
resource_class = ProductResource
list_filter = ('product_name','company__company_name')
list_display = ('....','....','...')
admin.site.register(Product,ProductAdmin)
进一步使用https://django-import-export.readthedocs.org/en/latest/installation.html