Django SimpleLazyObject

12,644

Solution 1

request.user, by default is of type SimpleLazyObject. To resolve it,

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

should be

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

If this does not resolve it, make sure you are logged in.

Solution 2

There is another option, may be this solution will help you fix it.

from django.contrib import auth

bookmark, created = Bookmark.objects.get_or_create(
        user = auth.get_user(request),
        link = link
        )
Share:
12,644
Tchec
Author by

Tchec

I want to learn every programming language. And just learning Python and Django. Hope one day, i will be a programmer.

Updated on June 27, 2022

Comments

  • Tchec
    Tchec almost 2 years

    When I try to submit, I get a TypeError:

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

    My 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 bookmarks.
                bookmark, created = Bookmark.objects.get_or_create(
                    user = request.user,
                    link = link
                    )
                # Update bookmark title.
                bookmark.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)
    

    Please guide me. Thank you.

  • Tchec
    Tchec almost 11 years
    Ok, I did, as u have writen here. But now I am getting another error.: "Cannot assign None: "Bookmark.user" does not allow null values."
  • karthikr
    karthikr almost 11 years
    looks like you are not logged in . are you ? use @login_required decorator
  • Tchec
    Tchec almost 11 years
    Okay, sorry i was not logged in. But now when i am, I am getting this error: Cannot assign "3": "Bookmark.user" must be a "User" instance
  • karthikr
    karthikr almost 11 years
    Really ? That is wierd. Now, try user = request.user without the id . Do you get the same issue?
  • Tchec
    Tchec almost 11 years
    Okay... That did it. Thank you! But can you please tell me what was the problem here. Was it just because authentication? And one more thing, when the tried to click on the Links or the bookmarks, they are not redirecting to its respective URL, just coming back to its user page. I am sorry, if that was too much. But thank you again!
  • karthikr
    karthikr almost 11 years
    request.user by default is not a user but SimpleLazyObject type. At the time of consuming the variable, it evaluates request.user to user instance if user is logged in. If user is not logged in, it evaluates to AnonymousUser object. Hence the issue. For the redirect URLs, most likely bookmark.get_absolute_url() is returning None
  • Tchec
    Tchec almost 11 years
    Thank you Karthik. I didn't get the last part about URL, but i think if i ask more, i will be of the topic. Thank you so much though!
  • karthikr
    karthikr almost 11 years
    Create a new question. You should get a good response for that. And do mark this answer as accepted if it was useful