81578

Django: Filtering drafts by user causes error

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'

Answer1:

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)

Answer2:

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?

Recommend

  • Rpy2: How to convert list of dictionaries to R data frame
  • Where does AngularJS store Cache data
  • Sorting a dictionary by an inner “attribute” dictionary key-value
  • How to animate a circle using bokeh
  • Recursively merge dicts so that elements with shared key are combined into a list
  • Combinations of a string with specific variable characters
  • split with no argument in perl
  • Removing non-english words from a sentence in python
  • Why is the plotting order not respected when I plot a Circle and a Multiline later?
  • Setting up Subversion Server on Mac OS mountain lion (have access via browser, but un-able to checko
  • Issue of feeding in feed_dict (Tensorflow)
  • How can I convert this tuple of tuples into a count of its elements?
  • how to set image's top and bottom in same size for android?
  • python re.compile and split with ÆØÅ charcters
  • Converting Dictionary to Dataframe with tuple as key
  • Pie-in-pie (Donut) charts
  • Java tree with columns
  • Bokeh custom ToolTips {safe} tag displays nothing
  • iPhone - UIWebView multiple locks
  • Xcode duplicated devices bug
  • How to get to older Xcode beta version?
  • Safari PHP form submission -file upload hangs
  • Python getting common name from URL using ssl.getpeercert()
  • Grails calculated field in SQL
  • RectangularRangeIndicator format like triangular using dojo
  • Bug in WPF DataGrid
  • How to add date and time under each post in guestbook in google app engine
  • TFS: Get latest causes slow project reloading
  • Javascript Callbacks with Object constructor
  • JSON with duplicate key names losing information when parsed
  • How to make Safari send if-modified-since header?
  • To display the title for the current loaction in map in iphone
  • Return words with double consecutive letters
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • How to stop GridView from loading again when I press back button?
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?