How can I get data fro QuerySet dict received via AJAX to Django View to create model objects ?
I have a model and want to create record based on the data recieved to my view from a AJAX call:
In models.py
--------------------------------------
class Items(models.Model):
item = models.CharField (max_length=128, blank=False,null=False)
qty = models.CharField (max_length=128, blank=False,null=False)
price = models.CharField (max_length=128, blank=False,null=False)
In views.py
--------------------------------------
if request.is_ajax():
if request.method == 'GET':
json_data = request.GET
print("LET'S DUMP THE DATA NOW!!")
data = json.dumps(json_data)
print(data)
Output to console:
I am recieving a dictionary from ajax to my view like this:
--------------------------------------
{"deals[0][Price]": "400", "deals[1][Item]": "Diet Donut", "deals[2][Price]": "180", "deals[1][Price]": "100", "deals[0][Item]": "Donut 4", "deals[2][Item]": "HazelNut Donut", "deals[1][Quantity]": "1", "deals[0][Quantity]": "4", "deals[2][Quantity]": "10"}
HTML-JS part
--------------------------------------
Page1.html
<script>
var items = [];
var qtys = [];
$(document).ready(function() {
var deal_collection = {
deals:[]
};
var jsonData = {};
// Getting data on a CLICK here for the variables
$('a[id^="tag"]').click(function() {
var index=$(this).index()-1;
var text = $(this).text();
var inv = {{ inv_all|safe }};
var item = inv[index].fields.item_name;
var qty = inv[index].fields.quantity;
var price = inv[index].fields.price;
// Adding to array
deal_collection.deals.push({
"Item":item,
"Quantity":qty,
"Price":price
});
});
});
//Saving deal_collection to localStorage so that it can be retireved on the Next page
$('#ajax_call').click(function(){
alert("AJAX Call Clicked");
console.log("AJAX Call clicked");
if (typeof(Storage) !== "undefined") {
localStorage.setItem('deal_array',JSON.stringify(deal_collection))
console.log("Data stored to storage")
} else {
// Sorry! No Web Storage support..
alert('No Storage available')
}
});
</script>
Page2.html:
<script>
var arr = JSON.parse(localStorage.getItem("deal_array"));
$.each(arr, function(i) {
console.log(arr[i]);
});
var msg = "Success !!!"
$.ajax({
url: '{% url 'ajax' %}',
type: 'GET',
data: arr,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
alert(msg);
}
});
});
</script>
/r/django
https://redd.it/6vp3hu
I have a model and want to create record based on the data recieved to my view from a AJAX call:
In models.py
--------------------------------------
class Items(models.Model):
item = models.CharField (max_length=128, blank=False,null=False)
qty = models.CharField (max_length=128, blank=False,null=False)
price = models.CharField (max_length=128, blank=False,null=False)
In views.py
--------------------------------------
if request.is_ajax():
if request.method == 'GET':
json_data = request.GET
print("LET'S DUMP THE DATA NOW!!")
data = json.dumps(json_data)
print(data)
Output to console:
I am recieving a dictionary from ajax to my view like this:
--------------------------------------
{"deals[0][Price]": "400", "deals[1][Item]": "Diet Donut", "deals[2][Price]": "180", "deals[1][Price]": "100", "deals[0][Item]": "Donut 4", "deals[2][Item]": "HazelNut Donut", "deals[1][Quantity]": "1", "deals[0][Quantity]": "4", "deals[2][Quantity]": "10"}
HTML-JS part
--------------------------------------
Page1.html
<script>
var items = [];
var qtys = [];
$(document).ready(function() {
var deal_collection = {
deals:[]
};
var jsonData = {};
// Getting data on a CLICK here for the variables
$('a[id^="tag"]').click(function() {
var index=$(this).index()-1;
var text = $(this).text();
var inv = {{ inv_all|safe }};
var item = inv[index].fields.item_name;
var qty = inv[index].fields.quantity;
var price = inv[index].fields.price;
// Adding to array
deal_collection.deals.push({
"Item":item,
"Quantity":qty,
"Price":price
});
});
});
//Saving deal_collection to localStorage so that it can be retireved on the Next page
$('#ajax_call').click(function(){
alert("AJAX Call Clicked");
console.log("AJAX Call clicked");
if (typeof(Storage) !== "undefined") {
localStorage.setItem('deal_array',JSON.stringify(deal_collection))
console.log("Data stored to storage")
} else {
// Sorry! No Web Storage support..
alert('No Storage available')
}
});
</script>
Page2.html:
<script>
var arr = JSON.parse(localStorage.getItem("deal_array"));
$.each(arr, function(i) {
console.log(arr[i]);
});
var msg = "Success !!!"
$.ajax({
url: '{% url 'ajax' %}',
type: 'GET',
data: arr,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
alert(msg);
}
});
});
</script>
/r/django
https://redd.it/6vp3hu
reddit
How can I get data fro QuerySet dict received via AJAX... • r/django
I have a model and want to create record based on the data recieved to my view from a AJAX call: In...