Reputation: 8058
I'm trying to add multiple related objects to a parent object with Django.
The error i get: int() argument must be a string or a number, not 'Tag'
My code looks like this:
def ask(request):
form = AskQuestionForm
if request.method == 'POST':
form = AskQuestionForm(request.POST)
if form.is_valid():
tags = request.POST.getlist('tags')
# Category
qcat = Category.objects.filter(id=request.POST.get('category')).first()
o = Question.objects.create(
title = request.POST.get('title'),
body = request.POST.get('body'),
category = qcat,
user = request.user
)
for t in tags:
rt = Tag.objects.get_or_create(word=t)
o.tags.add(rt)
return redirect('questions.index')
return render(request, 'questions/ask.html', {
'form' : form
})
I want to add tags to question object. What am I doing wrong?
Upvotes: 0
Views: 200
Reputation: 45555
get_or_create()
returns a tuple of (object, created)
. So change the tag creation to:
rt, _ = Tag.objects.get_or_create(word=t)
Upvotes: 2