2

I'm trying to filter through a set of drafts objects in a database using the request.user variable. They are For some reason I get the error listed bellow. How can I fix this bug?

Function:

def posting_draft(request):
    user = request.user
    user_drafts = Draft.objects.filter(user = user)
    drafts = dict()
    for d in user_drafts:
        drafts[d.title] = d.id
    return render_to_response('posting_draft.html', {'STATIC_URL':STATIC_URL, 'draft_l' : drafts})

Error:

int() argument must be a string or a number, not 'SimpleLazyObject'
sinθ
  • 11,093
  • 25
  • 85
  • 121

2 Answers2

4

Since request.user is a SimpleLazyObject until it is accessed. Try changing your query to the following:

user_drafts = Draft.objects.filter(user = user.pk)
Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
  • Could you explain what .pk does? – sinθ Aug 09 '12 at 03:10
  • 1
    It's the same as `user.id`, but will work even if you have a different field set as the `primary_key`. When you call it, it will force the `SimpleLazyObject` to become populated. It does the so that the data will only be fetched from the DB if you really need it, since DB lookups are relatively expensive. – Matt Williamson Aug 09 '12 at 03:13
3

The problem is in the line:

user = request.user

Read this post for more details request.user returns a SimpleLazyObject, how do I "wake" it?

Community
  • 1
  • 1
codegeek
  • 32,236
  • 12
  • 63
  • 63