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

Import related models without static foreign keys #69

Open
rez0n opened this issue Mar 4, 2021 · 4 comments
Open

Import related models without static foreign keys #69

rez0n opened this issue Mar 4, 2021 · 4 comments

Comments

@rez0n
Copy link

rez0n commented Mar 4, 2021

Hi, I need to import two related sheets without static foreign in the database.
Referring to an example from the docs my "Question" sheet always needs to be appended to the database with auto-id and "Choice" rows need to be foreign to the Question using rel_id (but not write rel_id to the database, it is needed only for build relations during import.
As a result, the single Question unique field is id that created automatically by database auto increment.

image
image

Thanks for any suggestions.

@rez0n
Copy link
Author

rez0n commented Mar 4, 2021

I've created this logic using plain pyexcel, but not sure how to obtain id of created question object to the choice_func using django-excel

        questions = pyexcel.get_sheet(
            file_name="sample-data.xls", name_columns_by_row=0, sheet_name='question')
        choices = pyexcel.get_sheet(
            file_name="sample-data.xls", name_columns_by_row=0, sheet_name='choice')

        for i in questions:
            obj = Question.objects.create(
                question_text=i[0],
                pub_date=i[1]

            )
            for c in choices:
                if c[4] == i[3]:
                    Choice.objects.create(
                        choice_text=c[1],
                        votes=c[3],
                        question=obj
                    )

@chfw
Copy link
Member

chfw commented Mar 7, 2021

@chfw
Copy link
Member

chfw commented Mar 7, 2021

if you use bulk creation, so that won't be possible. hence, you may want to do both in separate process.

@rez0n
Copy link
Author

rez0n commented Mar 7, 2021

Hi, it can't be queried from the database because at this moment question not yet created, but when it will be created it will get auto-increment ID and no known pk will be available.
The single available pk it is rel_id that accessible only during import, this field not will be imported to the database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants