0

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?

1 Answer 1

2

get_or_create() returns a tuple of (object, created). So change the tag creation to:

rt, _ = Tag.objects.get_or_create(word=t)
Sign up to request clarification or add additional context in comments.

2 Comments

what is the underscore?
It is just a name of the variable. You can name it created if you want: rt, created = ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.