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 #13

Closed
girijasameera opened this issue Jan 15, 2016 · 2 comments
Closed

Question #13

girijasameera opened this issue Jan 15, 2016 · 2 comments

Comments

@girijasameera
Copy link

In polls.py

def import_data(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
def choice_func(row):
print row[0]
q = Question.objects.filter(slug=row[0])[0]
row[0] = q
return row
if form.is_valid():
request.FILES['file'].save_book_to_database(
models=[
(Question, ['question_text', 'pub_date', 'slug'], None, 0),
(Choice, ['question', 'choice_text', 'votes'], choice_func, 0)
]
)
return HttpResponse("OK", status=200)
else:
return HttpResponseBadRequest()
else:

what does choice_fun do exactly?I didn't understand slug in Choice model.

@chfw
Copy link
Member

chfw commented Jan 15, 2016

First of all, Question has one or more Choices. Hence Choice has a foreign key (see code) to Question. When a spread sheet of such related data is imported, the question is how to keep the relationship.

Now back to your question, choice_func triesto find out which question the incoming choice row belongs to. In this example data, Question has "Unique Identifier"(slug) field and Choice refers to it as "Question".

Why 'slug'? it is defined in Question model. "Unique Identifier" in the spreadsheet is used for human readability while before importing it, this row ['question_text', 'pub_date', 'slug'] replaces those human readable column names in the matching sequence.

Does it help?

@girijasameera
Copy link
Author

Yes. Thank You.

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