I am a newbie in Django and I am building a quiz application. I want to be able to import some questions from a CSV file in Django using the import_export.admin method on the admin site.
When I try to import, the contents of my CSV file, which contains the list of questions, does not append to my existing list of questions. Instead, it throws an error,
invalid literal for int() with base 10: 'Independence'.
I have tried changing the format of the CSV file I am uploading from comma-separated Values to CSV UTF-8 (comma-delimited). I also tried using integers in the 'Category' column, but it gives a 'Traceback (most recent call last) Category matching query does not exist' error. How can I fix this?
My admin.py file:
class QuizAdmin(admin.ModelAdmin):
form = QuizAdminForm
list_display = ('title', 'category', )
list_filter = ('category',)
search_fields = ('description', 'category', )
class CategoryAdmin(admin.ModelAdmin):
search_fields = ('category', )
class MCQuestionAdmin(ImportExportModelAdmin):
list_display = ('content', 'category', )
list_filter = ('category',)
fields = ('content', 'category',
'figure', 'quiz', 'explanation', 'answer_order')
search_fields = ('content', 'explanation')
filter_horizontal = ('quiz',)
inlines = [AnswerInline]
My models.py file:
class CategoryManager(models.Manager):
def new_category(self, category):
new_category = self.create(category=re.sub('\s+', '-', category).lower())
new_category.save()
return new_category
class Category(models.Model):
category = models.CharField(verbose_name=_("Category"),
max_length=250, blank=True,
unique=True, null=True)
objects = CategoryManager()
class Meta:
verbose_name = _("Category")
verbose_name_plural = _("Categories")
def __str__(self):
return self.category
class Question(models.Model):
"""
Base class for all question types.
Shared properties placed here.
"""
quiz = models.ManyToManyField(Quiz,
verbose_name=_("Quiz"),
blank=True)
category = models.ForeignKey(Category,
verbose_name=_("Category"),
blank=True,
null=True, on_delete=models.CASCADE)
figure = models.ImageField(upload_to='uploads/%Y/%m/%d',
blank=True,
null=True,
verbose_name=_("Figure"))
content = models.CharField(max_length=1000,
blank=False,
help_text=_("Enter the question text that "
"you want displayed"),
verbose_name=_('Question'))
explanation = models.TextField(max_length=2000,
blank=True,
help_text=_("Explanation to be shown "
"after the question has "
"been answered."),
verbose_name=_('Explanation'))
objects = InheritanceManager()
class Meta:
verbose_name = _("Question")
verbose_name_plural = _("Questions")
ordering = ['category']
def __str__(self):
return self.content
def create_new_question(data):
questions = Question.objects.create_new_question(quiz=data['quiz'],
category=data['category'],
content=data['content'],
explanation=data['explanation'])
questions.save()
When I upload the file I get the error:
Invalid literal for int() with base 10: 'Independence'
When creating a question, for category, you should use the id of an existing category, not the string value or a random integer.