Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question - additional field #17

Closed
jsartre opened this issue Apr 5, 2016 · 3 comments
Closed

Question - additional field #17

jsartre opened this issue Apr 5, 2016 · 3 comments
Labels

Comments

@jsartre
Copy link

jsartre commented Apr 5, 2016

Hi everyone,

I still consider myself a newbie on django but i've been trying to toy around with django-excel which is working awesomely so far.

I would like to go a bit further and add a field (charfield) in my form that I would like to insert at the same time as the excel import and that will be the same for all rows.

Any directions you guys could point me to?
Thanks!

Joseph

@chfw
Copy link
Member

chfw commented Apr 5, 2016

Hi, Joseph

What I understood your intention is to insert some custom data from your form during the file import, whereas the tutorial shows only database import without 'salt'.

One idea is take advantage of row initializer to do custom change. The other idea is to use get_sheet( get_book )to obtain the pyexcel.Sheet( pyexcel.Book) instance, with which you can do data manipulations. Then you can use pyexcel.Sheet.save_to_django_model ( pyexcel.Book.save_to_django_models ) for the data import.

Hope it helps.

Chfw

@chfw chfw added the question label Apr 5, 2016
@jsartre
Copy link
Author

jsartre commented Apr 5, 2016

Hi Jaska,

Thank you for your answer. Yes you are correct, for every row that is imported, I would like to set one specific field.

For example:
if I have three columns in the excel file [number, name, address] I would like to set the extra field account_id in the database. All the rows ill have the same account_id.

I was trying to look at the first option that you proposed : initializer. However, I am exactly sure how to use this method. Should I pass a query? or just a specific parameter?

Thanks in advance.

Joseph

@chfw
Copy link
Member

chfw commented Apr 6, 2016

Under the first route, the initializer is expected to return a list of values for each new row/data entry, which is in the same order as the column_names.

The default assumption is that the number of column names matches the number of data columns and their order is the same. Your use case is, the 4th column is outside the spreadsheet.

Recommended route is to go with the second one: get a sheet out, populate 4th column and save to django model.

Hacking route is via first route, set column_names = [number, name, address, account_id](note, account_id should be the exact database field name), then in your row initializer, add: row.append(the_same_account_id_for_all), which append a 4th item onto the row. This could work out too.

@chfw chfw closed this as completed Apr 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants