Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango Piston Post Request Change strings to lists
    primarykey
    data
    text
    <p>I have a simple Django-Piston Handler that creates a new instance of a model and saves it.</p> <p>From the client, I am posting using Javascript Objects and JQuery.post.</p> <p>Upon inspecting the data with Firebug, the post string looks like this:</p> <pre><code>classification=&amp;organization=&amp;title=Formatting+Test+7&amp;description=test&amp;created_by=created_on=&amp;last_modified=&amp;csrfmiddlewaretoken=a8352bb51f88c271119a81228a5c291f </code></pre> <p>As you can see, only title, description, and csrfmiddlewaretoken are set.</p> <p>Inside the Piston Handler, at the top of the create method before any other code is run, I print out the request.POST:</p> <pre><code>print 'POST:', request.POST </code></pre> <p>Which prints the following in the Django Dev Server output:</p> <pre><code>POST: &lt;QueryDict: {u'description': [u'test'], u'classification': [u''], u'title': [u'Formatting Test 7'], u'created_by': [u''], u'created_on': [u''], u'last_modified': [u''], u'organization': [u''], u'csrfmiddlewaretoken': [u'a8352bb51f88c271119a81228a5c291f']}&gt; </code></pre> <p>As you can see, all of the data is in lists now. When I save the data using a Django Form, it converts the list to a string and when the data shows up on the client, instead of "Formatting Test 7", it displays "[u'Formatting Test 7']".</p> <p>Googling doesn't seem to return anyone with the same problem, and I have been using Piston for over 6 months and I have never seen an error like this before.</p> <p><strong>EDIT:</strong> As JPIC said, the error I thought I was experiencing was not an error, but by design. My problem was in extending the data in the QuerySet before I used it with a Django Form.</p> <p><strong>The Wrong Way:</strong> </p> <pre><code>some_initial_data = { 'created_by': creator.pk, 'organization': org.pk, 'classification': 7 } post = dict(request.POST) post.update(some_initial_data) form = MyModelForm(post) </code></pre> <p><strong>The Right Way:</strong> </p> <p>As described in the update method for QueryDict: <a href="https://docs.djangoproject.com/en/1.3/ref/request-response/#querydict-objects" rel="nofollow">https://docs.djangoproject.com/en/1.3/ref/request-response/#querydict-objects</a></p> <pre><code>some_initial_data = { 'created_by': creator.pk, 'organization': org.pk, 'classification': 7 } post = request.POST.copy() post.update(initial_data) form = MyModelForm(post) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload