Home > Enterprise >  Cannot resolve keyword 'location' into field
Cannot resolve keyword 'location' into field

Time:08-14

I installed django and I would like to use leaflet to display the position of my weather station.

I found that nice tutorial and I could complete with the exemple scripts

However, now I would like to apply that exercise to my project. I have a database with about less of one million of measures and I need to keep the structure of my database/table. I will do my best to describe my problem.

My problem start here (viewsets.py) and here . As I understand, the 'location' match to a field of my table, which should contain a value similar to this

'POINT(6.000251769184164 46.19337852885697)',0

because the type of the field is POINT. However, my database does not have a such field. Instead, I have a field station_lat (float(8.6)) and station_lng (float(8.6)), where I save the latitude and longitude. That the reason why, I have the error, because it can not find 'location'

Cannot resolve keyword 'location' into field. Choices are: fields_id_field, fields_id_field_id, id_station, installed, map, sensors, station_active, station_alt, station_archive, station_created, station_description, station_lat, station_lng, station_longname, station_name, station_order, stations_types_id_stations_type, ttn_app_id, ttn_dev_id, ttn_hardware_serial

As I have to use my database as the fields are defined, and as 'geo_field is mandatory, I first would like to know, if in my serializerer.py file, I can adapt the 'geo_field' to search for the fields 'station_lat' and 'station_lng) and convert it in the following format

'POINT(6.000251769184164 46.19337852885697)',0

I think, I am going to have a similar issue to solve in the viewsets.py field, but for now if I comment that line, it does not generate an error.

I hope that few words are enough to understand my worries, because I am deseperated :(

Many thanks for any tips and hints, or solution you can provide to me.

CodePudding user response:

You can keep the location field in your model Stations, and just change it in the save() method to satisfy the POINT format:

from django.contrib.gis.db.models import PointField
from django.contrib.gis.geos import Point

class Stations(models.Model):
    ...
    location = PointField(blank=True, null=True, srid=4326)
    station_lat = models.FloatField(blank=True, null=True)
    station_lng = models.FloatField(blank=True, null=True)
    ...
    class Meta:
        managed = False
        db_table = 'stations'

    def save(self, *args, **kwargs):
        self.location = Point(float(self.station_lng), float(self.station_lat))
        super(Stations, self).save(*args, **kwargs)

CodePudding user response:

So many thank, it's really better, but somethings is going well :).

I modified my models files as you proposed.

I needed to create a field 'location' into my database

Now, this https://github.com/ecosensors/Django/blob/main/EcoSensors/console/map/serializers.py#L24 do not generate an error great!

and then when I load my pages and I can see the right structure (See image)

However, as you can see the geometry entry is null. Which (may be) make sens, because, in my DB, the field 'location' has no value, and have the NULL value.

does the save() will update the field 'location'?.

I guess yes!

May I ask you to confirm that

If I have to create a filed 'location' in my DB/table, does the save() will update the field 'location'. If, it's the case, the field 'location', will it updated with the same value, each time I refresh the page?

So many thank for your help, that helped me to go one step ahead. (I am a beginer)

Have a nice week-end

CodePudding user response:

It works, thanks, excepted for one things. I had to create the field 'location'. I had a problem while creating that field and I solve it following that article

I can see only one marker in my map, but in reallity, all markers are showed, but all are at the same location. That the reason why I see only one marker.

If now I look at the serializer, it returns me that

{
    "type": "FeatureCollection",
    "features": [
        {
            "id": 1,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-1",
                "station_lat": 46.187164,
                "station_lng": 5.997526
            }
        },
        {
            "id": 2,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-2",
                "station_lat": 46.173527,
                "station_lng": 6.003618
            }
        },
        {
            "id": 3,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-3",
                "station_lat": 46.186268,
                "station_lng": 5.997424
            }
        },
        {
            "id": 4,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-4",
                "station_lat": 46.173153,
                "station_lng": 6.003011
            }
        },
        {
            "id": 5,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-5",
                "station_lat": 46.187016,
                "station_lng": 5.997001
            }
        },
        {
            "id": 6,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-6",
                "station_lat": 46.173393,
                "station_lng": 6.004297
            }
        },
        {
            "id": 7,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-7",
                "station_lat": 46.186146,
                "station_lng": 5.997017
            }
        },
        {
            "id": 8,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-8",
                "station_lat": 46.172722,
                "station_lng": 6.00308
            }
        },
        {
            "id": 9,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-9",
                "station_lat": 46.184677,
                "station_lng": 5.997972
            }
        },
        {
            "id": 10,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-10",
                "station_lat": 46.182545,
                "station_lng": 5.999088
            }
        },
        {
            "id": 11,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-11",
                "station_lat": 46.185596,
                "station_lng": 5.996834
            }
        },
        {
            "id": 12,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-12",
                "station_lat": 46.182682,
                "station_lng": 6.003728
            }
        },
        {
            "id": 13,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-13",
                "station_lat": 46.184441,
                "station_lng": 5.999066
            }
        },
        {
            "id": 14,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-14",
                "station_lat": 46.172466,
                "station_lng": 6.00361
            }
        },
        {
            "id": 15,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-15",
                "station_lat": 46.17223,
                "station_lng": 6.002607
            }
        },
        {
            "id": 16,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-16",
                "station_lat": 46.183529,
                "station_lng": 6.006996
            }
        },
        {
            "id": 17,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-17",
                "station_lat": 46.172356,
                "station_lng": 6.004136
            }
        },
        {
            "id": 18,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-18",
                "station_lat": 46.179119,
                "station_lng": 6.004328
            }
        },
        {
            "id": 20,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-20",
                "station_lat": 46.182545,
                "station_lng": 5.999088
            }
        },
        {
            "id": 21,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-21",
                "station_lat": 46.185722,
                "station_lng": 5.997623
            }
        },
        {
            "id": 22,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-22",
                "station_lat": 46.17284,
                "station_lng": 6.002612
            }
        },
        {
            "id": 23,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-23",
                "station_lat": 46.184319,
                "station_lng": 5.998923
            }
        },
        {
            "id": 24,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-24",
                "station_lat": 46.184345,
                "station_lng": 5.998887
            }
        },
        {
            "id": 25,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-25",
                "station_lat": 46.183109,
                "station_lng": 6.006879
            }
        },
        {
            "id": 26,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-26",
                "station_lat": 46.183361,
                "station_lng": 6.006943
            }
        },
        {
            "id": 27,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-27",
                "station_lat": 46.217918,
                "station_lng": 6.016133
            }
        },
        {
            "id": 28,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-28",
                "station_lat": 46.217934,
                "station_lng": 6.014958
            }
        },
        {
            "id": 29,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-29",
                "station_lat": 46.217575,
                "station_lng": 6.01465
            }
        },
        {
            "id": 30,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-30",
                "station_lat": 46.217575,
                "station_lng": 6.01465
            }
        },
        {
            "id": 31,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-31",
                "station_lat": 46.184528,
                "station_lng": 5.999068
            }
        },
        {
            "id": 32,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-32",
                "station_lat": 46.183529,
                "station_lng": 6.006996
            }
        },
        {
            "id": 33,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-33",
                "station_lat": 46.183769,
                "station_lng": 6.007091
            }
        },
        {
            "id": 34,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-34",
                "station_lat": 46.183132,
                "station_lng": 6.006629
            }
        },
        {
            "id": 35,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-35",
                "station_lat": 46.183407,
                "station_lng": 6.006712
            }
        },
        {
            "id": 36,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-36",
                "station_lat": 46.183582,
                "station_lng": 6.006712
            }
        },
        {
            "id": 37,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-37",
                "station_lat": 46.183762,
                "station_lng": 6.006895
            }
        },
        {
            "id": 38,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-38",
                "station_lat": 46.183144,
                "station_lng": 6.006361
            }
        },
        {
            "id": 39,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-39",
                "station_lat": 46.183449,
                "station_lng": 6.006433
            }
        },
        {
            "id": 40,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-40",
                "station_lat": 46.183567,
                "station_lng": 6.006497
            }
        },
        {
            "id": 41,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-41",
                "station_lat": 46.183796,
                "station_lng": 6.006625
            }
        },
        {
            "id": 42,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-42",
                "station_lat": 46.183132,
                "station_lng": 6.007131
            }
        }
    ]
}

We can see geometry, which has always the same value

"geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },

It's look like the field 'location' is not updated. Why? May be, I do not really understand this part here

def save(self, *args, **kwargs):
        self.location = Point(float(self.station_lng), float(self.station_lat))
        super(Stations, self).save(*args, **kwargs)

When is performed the saved/updated? Should I run ./manager.py makemigrations and ./manager.py migrate to update the 'location' field?

Many thanks!!!

  • Related