How to convert a querydict with multiple objects into lists

When I send an array of JavaScript objects to a Django view via AJAX, the object structure is as follows:

[{'oid':'id1','oiid':'iid1'},{'oid':'id2','oiid':'iid2'}]

This is how my AJAX request looks like:

....
type : "POST",
data : {action:'pack_orders',
        order_dict:$checkedRows},
....

In the Django view, the request is received as a QueryDict with this format:

<QueryDict : {'order_dict[0][oid]':'id1','order_dict[0][oiid]':'iid1',
              'order_dict[1][oid]':'id2','order_dict[1][oiid]':'iid2'}>

The challenge is to extract lists from the QueryDict in the Django view like so:

oid_list = ['id1','id2']
oiid_list = ['iid1','iid2']

The output in the views.py file should be:

{'order_dict[0][oid]':'id1','order_dict[0][oiid]':'iid1',
 'order_dict[1][oid]':'id2','order_dict[1][oiid]':'iid2'}

An additional note: The output from the statement print dict(request.POST) has been added to the views.py file.

Answer №1

Ensure you are properly sending the order_dict after converting it to a JSON string. If not, I recommend doing so. Receive it as a JSON string in your view, then use json.loads() on it. After this process, you will have a list of dictionaries.

Let's assume you save it in a variable named data, you can then execute the following:

oid_list = [x.get('oid') for x in data]
oiid_list = [x.get('oiid') for x in data]

Output:

oid_list - ['id1', 'id2']
oiid_list - ['iid1', 'iid2']

This should work for you. It might not be the most advanced solution, but it is a straightforward one.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a multi-tiered cascading menu in a web form: Harnessing the power of

In my form, there is a field called 'Protein Change' that includes a multi-level dropdown. Currently, when a user selects an option from the dropdown (for example, CNV->Deletion), the selection should be shown in the field. However, this funct ...

In React JS, the material-ui Accordion component's onchange function is failing to retrieve the current value

I am encountering an issue with the @material-ui Accordion where the onChange function is not capturing the current value. For example, when I click on the panel1 icon, it opens panel2 instead of taking the current value on icon click. I have provided the ...

What is the most efficient way to save a document in mongoose based on a specific user's

Is there a way to ensure that when saving a template, it is associated with the user id? I have added a reference to the templateSchema for the User. User.model.js var UserSchema = new mongoose.Schema({ _id: { type: String, required: true, index: {uniq ...

The functionality of AJAX is triggered only on the second click

On my index.html page, I'm attempting to implement a basic AJAX functionality. When clicking on an anchor tag, the text will be sent to a PHP page called data.php, which will then display the clicked page. The code is functional, but there seems to be ...

Implementing the Infinite Scroll feature with 'react-infinite-scroll-component' on local JSON data: A step-by-step guide

Currently experimenting with frontEnd development, I have incorporated the 'react-infinite-scroll-component' as a dependency. My goal is to apply this effect to my local Json data without relying on any API calls at this stage. I'm encounter ...

What causes the child component to re-render when only the prop is changed and useCallback is used?

Child component will only re-render if its prop (numberModifier) is changed. The numberModifier uses useCallback with no dependencies, so it remains constant. To test this, I alter the value of "online" in the Application component which is a prop of Pare ...

Do we need to use the "new" keyword when using ObjectID in a MongoDB query

Recently, I was immersed in a Typescript web project that involved the use of MongoDB and ExpressJS. One particular task required me to utilize a MongoDB query to locate and delete a document using the HTTP DELETE method. However, during the process of exe ...

Information is not appearing in the table

I'm having trouble displaying data in a table format. The issue arises when I try to fetch data from a JSON file using a custom service. The fetched data is then inserted into the $rootScope object. However, when I preview the view, it appears blank ...

Discovering a specific property of an object within an array using Typescript

My task involves retrieving an employer's ID based on their name from a list of employers. The function below is used to fetch the list of employers from another API. getEmployers(): void { this.employersService.getEmployers().subscribe((employer ...

show a fresh new page using react router

I have recently developed a mobile app that showcases a collection of movies. Currently, it is static and I am looking to implement a react router for navigation. Specifically, I want the user to be directed to a detail page for a TV Show when they click o ...

Refreshing Form in Angular 2

When I remove a form control from my form, it causes the form to always be invalid. However, if I delete a character from another input field and then add the same character back in (to trigger a change event), the form becomes valid as expected. Is ther ...

navigation through sibling views using query-based route in ui-router

Imagine an app with two sides: "left" and "right", each with tabs. The application's URL structure is formatted like this: app/splitscreen/?leftTab=1&rightTab=1 Currently, the HTML template is set up as follows: <!-- splitscreen.tpl.html --& ...

Dragend event for images does not trigger in webkit when using TinyMCE

When using the TinyMCE editor, I tried binding the dragend event on images with the following code: _imagePlugin.editor.dom.bind(_imagePlugin.editor.dom.select('img'), 'dragend', function(){console.log('aaaa');}); Oddly enou ...

AngularJS: Utilizing nested ng-repeats for dynamic data rendering

Currently, I am utilizing the ng-repeat directive in conjunction with the ng-repeat-start/ng-repeat-end options. Within the main loop, there is an additional inner ng-repeat. However, the problem lies in the fact that the outer ng-repeat-start declaration ...

Creating effective test cases for Angular JS controllers

Our team has recently taken on the task of writing test cases for our application, specifically focusing on controllers. Utilizing Mocha, Chai, and Sinon libraries, we are looking for guidance on how to effectively write these test cases. We have shared a ...

The functionality of the String prototype is operational in web browsers, but it encounters issues

Version: 8.1.0 The prototype I am working with is as follows: String.prototype.toSlug = function () { return (<string>this) .trim() .toLowerCase() .replace(/\s+/g, '-') .replace(/[^\w\-]+/g, '') ...

A guide to displaying JSON data with Ajax

I am diving into the world of Ajax and feeling a bit puzzled about extracting all values from a specific source. <html> <head> <script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></sc ...

The React Router Dom V6 triggers loaders as soon as the router is initialized

I'm currently working on implementing routing into my application using react-router-dom V6, specifically utilizing the new createBrowserRouter function and RouterProvider component. The issue I'm facing is that when attempting to integrate a lo ...

What is the reason behind JSLint's preference for x === "undefined" over typeof x == "undefined"?

I'm feeling lost when it comes to JSLint. Initially, my code checked if div:jqmData("me") was undefined in this way: if ( typeof el.jqmData("me") == "undefined" ? el.not(':jqmData(panel="main")').length > 0 : el.not(':jqm ...

Updating a JSON array by including a new key-value pair using Javascript

Below is a json string that needs to be converted into a new Json Array: Raw Data: [ ["yrxqBHmPkNhZ60_eab97ebf-c2a3-40a5-972a-91597ad9a4ca_99371", "SUCCEEDED", "2023-08-31T21:59:31.325000+05:30", "2023-08-31T22:13:42.165000+05:30"], ["yrxqBHmPkNhZ ...