[Answered ]-Django Error ''Field 'id' expected a number but got <Transfernews: Transfernews object (1)>.''

1👍

In your view you should work with:

new_comment.transfernews_id = news

or:

new_comment.transfernews = Transfernews.objects.get(id=news)

but the latter is less efficient since you make an extra database query.

You thus can rewrite the view to:

from django.contrib.auth.decorators import login_required

@login_required
def transfer_targets(request):
    transfernews = Transfernews.objects.all()
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            news = request.POST['transfer_id']
            form.instance.user = request.user
            form.instance.transfernews_id = news
            form.save()
            return redirect(request.path_info)
    else:
        form = CommentForm()
    return render(request, 'transfernews.html', {'transfernews': transfernews, 'form': form})

Note: You can limit views to a view to authenticated users with the
@login_required decorator [Django-doc].

Leave a comment