-
-
Notifications
You must be signed in to change notification settings - Fork 87
Closed
Description
Hello and first of all, thanks for this package !
I'm having issue when trying to import data with a foreign key field. The code doesnt raise any error, but nothing is saved in database. I've tried using custom initialize as in the documentation, however though I obtain correct values, no object is saved in database. When trying the same code with entities without foreign keys, everything goes fine.
Please find attached my function code. Thanks in advance for any help.
ps : here is what print(row) prints : (types and values are as expected)
['SCHNEIDER ELECTRIC', 'A9L16577', 'v8', 'Z130', 'B', 'aaa', 1, 'dummy', <Groupe: Serveurs>]
`
def import_xls(item, form):
if form.is_valid():
if request.FILES['file'].content_type.find('spreadsheet') == -1:
messages.add_message(request, messages.ERROR, 'Le fichier n\'est pas un tableur !')
return
fields = []
temp = item._meta.get_fields()
for field in temp:
if not field.name.startswith("article") and not field.name == 'id' and not isinstance(field,ManyToManyField)and not isinstance(field, ManyToManyRel):
fields.append(field.name)
if item == ArticleHW or item == ArticleSW: #no foreign keys, everything goes fine
try:
request.FILES['file'].save_to_database(model=item, mapdict=fields)
messages.add_message(request, messages.SUCCESS, 'Enregistrement : Succès')
except ValueError:
messages.add_message(request, messages.ERROR, 'Erreur : Le nombre de champs ne correspond pas !')
elif item == Equipement:
try:
def set_group(row):
group = Groupe.objects.get(id=int(row[8]))
row [8] = group
print(row)
return row
#this is not saving
request.FILES['file'].save_to_database(model=item, mapdict=fields, initializer=set_group)
messages.add_message(request, messages.SUCCESS, 'Enregistrement : Succès')
except ValueError as e:
print(e)
messages.add_message(request, messages.ERROR, 'Erreur : Le nombre de champs ne correspond pas !') `
Metadata
Metadata
Assignees
Labels
No labels