You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
The text was updated successfully, but these errors were encountered: