def home2 (request , long, lat):
dat=item.objects.exclude(radius=0).order_by('-qualification')
if dat:
data=[]
for i in dat:
distance = math.sqrt(math.pow(i.latitud-lat, 2) + math.pow(i.long-long, 2) )
if distance < i.radius:
data.append(i)
#Here the doubt begins because I have to take everything to JSON format to be sent
array={}
for data_format in data:
first={"item":data_format.item, "name":data_format.provider.name,
"days": data_format.days_of_works, "hour_ini":data_format.hour_init, "hour_end":data_format.hour_end,
"qualification":data_format.qualification},
array+={i:first}
return JsonResponse(array, safe=False)
I’m having a hard time how the array variable needs to be arranged to have a proper JSON format
No, I do not. I have to send all the objects of the item model that have the smallest distance to the radius. This is why I do the if distance <dat [i] .radius comparison.
If the condition is met, I add that item element to an array that I call data. But what I want is to send it ordered. This is the first element, this is the second and so on … And I think the best way to do it is using a JSON format
Let me guess, are you looking at the object you receive either by using the ‘alert’ dialog box or printing it in the console?
You actually are receiving the data as a JSON object, your JavaScript at that point has already converted it from a character string representation into a JavaScript object. What you’re seeing is the generic representation that JavaScript supplies for an object. So yes, you are getting what you’re expecting to get. It’s how you’re trying to display it that is incorrect.
If you need further assistance at this point, we would need to see the JavaScript code being used to retrieve this data and how it’s trying to use it after being received.
Rather than looking at your console log, look at the network tab, and examine the responses you’re getting from the server. That will show you the data being sent from the server before JavaScript has a chance to mangle it. If there is a problem with what’s being returned, it will show up there.