Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

django - int() argument must be a string or a number, not 'SimpleLazyObject'

I got, following Error messages,

TypeError at /save/ int() argument must be a string or a number, not 'SimpleLazyObject'

While executing following form.


views.py

def bookmark_save_page(request):
    if request.method == 'POST':
        form = BookmarkSaveForm(request.POST)
        if form.is_valid():
            # create or get link
            link, dummy = Link.objects.get_or_create(
                    url = form.cleaned_data['url']
                    )
            # create or get bookmark
            bookmark, created = Bookmark.objects.get_or_create(
                    user=request.user,
                    link=link
                    )
            # update bookmarks title
            bookmarks.title = form.cleaned_data['title']
            # if the bookmark is being updated, clear old tag list.
            if not created:
                bookmark.tag_set.clear()
            # create new tag list
            tag_names = form.cleaned_data['tags'].split()
            for tag_name in tag_names:
                tag, dummy = Tag.objects.get_or_create(name=tag_name)
                bookmark.tag_set.add(tag)
            # save bookmark to database.
            bookmark.save()
            return HttpResponseRedirect(
                    '/user/%s/' % request.user.username
                    )
    else:
        form = BookmarkSaveForm()
    variables = RequestContext(request, {
        'form': form
    })
    return render_to_response('bookmark_save.html', variables)

I thought I got error because I passed link at

bookmark, created = Bookmark.objects.get_or_create(
                    user=request.user,
                    link=link
                    )

But I can not figure out how to fix it. How can I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The likely cause is that you're setting user = request.user, where request.user is not a real User object, but a SimpleLazyObject instance. See django: Purpose of django.utils.functional.SimpleLazyObject? for more details, but using request.user.id should fix your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...