Search in database and send them by JSON format

The problem I have is that I have an array of objects, of which I have to have certain fields of them to be sent by JSON format.

This is the model:

class item (models.Model):
    items = models.CharField(max_length=25)
    provider = models.ForeignKey(serviceProvider, on_delete=models.CASCADE, null=True)
    radius = models.FloatField(default=None)
    posicion_lat = models.FloatField(default=None)
    posicion_long= models.FloatField(default=None)
    description= models.TextField(blank = True)
    days_of_works = models.CharField(max_length=25)
    hour_init = models.TimeField()
    hour_end = models.TimeField()
    picture1=models.ImageField(default=None, blank=True)	
    picture3=models.ImageField(default=None, blank=True)	

    def __str__(self):
        return str(self.radius)

And this is the function:

def home2 (request , long, lat):

    if dat:
        for i in dat:
            distance = math.sqrt(math.pow(i.latitud-lat, 2) + math.pow(i.long-long, 2) )
            if distance < i.radius:
             #Here the doubt begins because I have to take everything to JSON format to be sent
        for data_format in data:
            first={"item":data_format.item, "name", 
            "days": data_format.days_of_works, "hour_ini":data_format.hour_init, "hour_end":data_format.hour_end,
        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

I don’t understand what your actual problem is.

  • Can you send a JSON response?
  • If yes, do you need to send it with some specific structure?
  • If your problem is here, could you post an example of the contents of data before entering the for i in data loop?
1 Like

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

I’m doing some assumptions based on the code:

  • dat is a Queryset of Item.
  • data is a Queryset of Item that have the smallest distance to the radius.

This depends on what order you want to give it. Based on a field? Remember you have order_by queryset method.

Client receives [Object Object] and not data in JSON representation

This would be the code of interest:

    for datos in datos_de_proveedores:
        "dias": datos.days_of_works, "hora_ini":datos.hour_init, "hora_fin":datos.hour_end,
        "calificacion":datos.qualification  }
    return JsonResponse(array, safe=False)

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.


Yes, I do. This is how I show it:

     axios.get(url+value).then((resp: { data: any; }) => {

And this is the Django code:

    for datos in datos_de_proveedores:
        "dias": datos.days_of_works, "hora_ini":datos.hour_init, "hora_fin":datos.hour_end,
        "calificacion":datos.qualification  }
    return JsonResponse(array, safe=False)

But instead, I can see the following information correctly in the client, with JSON notation:

            data = [{"name":, "last_name": datos.last_name,
            "qualification": datos.qualification,},
            {"client_picture": imagen['client_picture']}]

            return JsonResponse(data, safe=False)

This is how I display in console that:

     axios.get(url+value).then((resp: { data: any; }) => {

What is the difference, why in the first it works and in the second it does not?

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.

1 Like