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 django how to filter import_export file
No comments:
Post a Comment