How do i filter the CustomerSectionResource models? in my case all data save in the database export in the excel, I just want to select certain data to export in excel. i want to filter just like this
Ex.
company = FmCustomerUsers.objects.filter(user=request.user.id)
obj = FmCustomerSection.objects.filter(
fmCustomerID__company_name__in=company.values_list('fmCustomerID__company_name'))
how? help me guys....
resources.py
class FmCustomerSectionResource(resources.ModelResource):
fmCustomerID = fields.Field(attribute='customer', column_name='customer',
widget=ForeignKeyWidget(FmCustomer))
class Meta:
model = FmCustomerSection
fields = ('fmCustomerID', 'section', 'inputdate', 'inputBy', 'modifyDate', 'modifyBy', 'status')
views.py
from tablib import Dataset
from .resources import FmCustomerSectionResource
def import_Section(request):
if request.method == 'POST':
file_format = request.POST['file-format']
company = FmCustomerUsers.objects.filter(user=request.user.id)
product_resource = FmCustomerSectionResource.objects.filter(
fmCustomerID__company_name__in=company.values_list('fmCustomerID__company_name'))
dataset = Dataset()
new_city = request.FILES['importData']
if file_format == 'XLS':
imported_data = dataset.load(new_city.read(), format='xls')
result = product_resource.import_data(dataset, dry_run=True)
elif file_format == 'CSV':
imported_data = dataset.load(new_city.read(), format='csv')
# Testing data import
result = product_resource.import_data(dataset, dry_run=True)
if not result.has_errors():
# Import now
product_resource.import_data(dataset, dry_run=False)
return redirect('Section')
def export_Section(request):
if request.method == 'POST':
# Get selected option from form
file_format = request.POST['importData']
product_resource = FmCustomerSectionResource()
dataset = product_resource.export()
if file_format == 'CSV':
response = HttpResponse(dataset.csv, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="exported_data.csv"'
return response
elif file_format == 'XLS':
response = HttpResponse(dataset.xls, content_type='application/xls')
response['Content-Disposition'] = 'attachment; filename="exported_data.xls"'
return response
return redirect('Section')
from tablib import Dataset
from .resources import FmCustomerSectionResource
def import_Section(request):
if request.method == 'POST':
file_format = request.POST['file-format']
company = FmCustomerUsers.objects.filter(user=request.user.id)
product = FmCustomerSection.objects.filter(
fmCustomerID__company_name__in=company.values_list('fmCustomerID__company_name'))
product_resource = FmCustomerSectionResource()
dataset = product_resource .exclude(product)
new_city = request.FILES['importData']
if file_format == 'XLS':
imported_data = dataset.load(new_city.read(), format='xls')
result = product_resource.import_data(dataset, dry_run=True)
elif file_format == 'CSV':
imported_data = dataset.load(new_city.read(), format='csv')
# Testing data import
result = product_resource.import_data(dataset, dry_run=True)
if not result.has_errors():
# Import now
product_resource.import_data(dataset, dry_run=False)
return redirect('Section')
def export_Section(request):
if request.method == 'POST':
# Get selected option from form
file_format = request.POST['importData']
product_resource = FmCustomerSectionResource()
dataset = product_resource.export()
if file_format == 'CSV':
response = HttpResponse(dataset.csv, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="exported_data.csv"'
return response
elif file_format == 'XLS':
response = HttpResponse(dataset.xls, content_type='application/xls')
response['Content-Disposition'] = 'attachment; filename="exported_data.xls"'
return response
return redirect('Section')
I hope this question is almost identical to this one, django-import-export how to skip import some rows based on current user login?
You can use the
skip_row(...)
--(Doc) method, as you mentioned.But, the
skip_row(...)
method doesn't provide any hooks to therequest.user
, so, we are doing a simple hack to get the requested user inskip_row()
by overriding theimport_data(...)
from import_export.resources import ModelResource class BookResource(ModelResource): class Meta: model = Book def import_data(self, *args, **kwargs): return super().import_data(*args, **kwargs)
You must update the skip_row(...)
method to get the desired result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.