i want to append a column to the imported excel table, this column will contain the foreignKey to a model (the same in all rows, this foreignKey is obtained from the same form as the .xlsx), but i can't make it work. If i upload the excel with the column and the id it works perfectly. This is my code: views.py
def article_import(request):
if request.method == 'POST':
provider_id = request.POST['provider_id']
article_resource = ArticleResource()
dataset = Dataset()
nuevos_articulos = request.FILES['xlsfile']
data_to_import = dataset.load(nuevos_articulos.read())
result = article_resource.import_data(dataset, dry_run=True, provider_id=provider_id) #Test data
if not result.has_errors():
article_resource.import_data(dataset, dry_run=False, provider_id=provider_id) #Import data
return redirect('dash-articles')
return redirect('/dashboard/articles/?error=True')
else:
providers = Provider.objects.all().order_by('razon_social')
return render(request, 'import/articles_import.html', {'providers': providers})
resource.py
class ArticleResource(resources.ModelResource):
class Meta:
model = Article
skip_unchanged = True
report_skipped = False
import_id_fields = ('codigo',)
def before_import(self, dataset, using_transactions, dry_run, **kwargs):
dataset.append_col([kwargs['provider_id'],], header='proveedor')
print(dataset)
return super().before_import(dataset, using_transactions, dry_run, **kwargs)
The code in "before_import" is what i was testing but i can't make work the "append_col" function. I will apreciate the help, and if there is a better solution please tell me, thanks!!!