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
)
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, 2022Comments
-
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.
-
Aamir Rind almost 11 yearspossible duplicate of int() argument must be a string or a number, not 'SimpleLazyObject'
-
-
Tchec almost 11 yearsOk, 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 almost 11 yearslooks like you are not logged in . are you ? use
@login_required
decorator -
Tchec almost 11 yearsOkay, 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 almost 11 yearsReally ? That is wierd. Now, try
user = request.user
without theid
. Do you get the same issue? -
Tchec almost 11 yearsOkay... 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 almost 11 years
request.user
by default is not auser
butSimpleLazyObject
type. At the time of consuming the variable, it evaluatesrequest.user
touser
instance if user is logged in. If user is not logged in, it evaluates toAnonymousUser
object. Hence the issue. For the redirect URLs, most likelybookmark.get_absolute_url()
is returningNone
-
Tchec almost 11 yearsThank 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 almost 11 yearsCreate a new question. You should get a good response for that. And do mark this answer as accepted if it was useful