簡體   English   中英

無法將關鍵字“位置”解析為字段

[英]Cannot resolve keyword 'location' into field

我安裝了 django 並且我想使用 leaflet 來顯示我的氣象站的 position。

我發現了很好的教程,我可以用示例腳本完成

但是,現在我想將該練習應用到我的項目中。 我有一個包含大約一百萬個度量的數據庫,我需要保留我的數據庫/表的結構。 我會盡力描述我的問題。

我的問題從這里開始(viewsets.py)和這里 據我了解,“位置”與我的表的字段匹配,該字段應包含與此類似的值

'POINT(6.000251769184164 46.19337852885697)',0

因為字段的類型是 POINT。 但是,我的數據庫沒有這樣的字段。 相反,我有一個字段station_lat (float(8.6)) 和station_lng (float(8.6)),我在其中保存緯度和經度。 那就是為什么,我有錯誤,因為它找不到“位置”

無法將關鍵字“位置”解析為字段。 選項有:fields_id_field、fields_id_field_id、id_station、已安裝、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

由於我必須使用我的數據庫來定義字段,並且由於“geo_field”是強制性的,所以我首先想知道,如果在我的serializerer.py 文件中,我可以調整“geo_field”來搜索“station_lat”字段和 'station_lng) 並將其轉換為以下格式

'POINT(6.000251769184164 46.19337852885697)',0

我想,我將在 viewsets.py 字段中解決類似的問題,但現在如果我評論該,它不會產生錯誤。

我希望幾句話足以理解我的擔憂,因為我很絕望:(

非常感謝您提供給我的任何提示和提示或解決方案。

您可以將location字段保留在 model Stations中,只需在save()方法中更改它以滿足 POINT 格式:

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)

非常感謝,它真的更好,但事情進展順利:)。

我按照您的建議修改了我的模型文件。

我需要在我的數據庫中創建一個字段“位置”

現在,這個https://github.com/ecosensors/Django/blob/main/EcoSensors/console/map/serializers.py#L24不會產生很大的錯誤!

然后當我加載我的頁面時,我可以看到正確的結構(見圖)

但是,如您所見,幾何條目是 null。 哪個(可能)有意義,因為在我的數據庫中,“位置”字段沒有值,並且具有 NULL 值。

save() 是否會更新“位置”字段?

我猜是!

我可以請你確認一下嗎

如果我必須在我的數據庫/表中創建一個歸檔的“位置”,save() 是否會更新字段“位置”。 如果是這種情況,“位置”字段是否會在每次刷新頁面時更新為相同的值?

非常感謝您的幫助,這幫助我領先一步 go。 (我是初學者)

周末愉快

它有效,謝謝,除了一件事。 我必須創建字段“位置”。 我在創建該字段時遇到了問題,我按照那篇文章解決了它

我在 map 中只能看到一個標記,但實際上,所有標記都顯示出來了,但都在同一個位置。 這就是為什么我只看到一個標記的原因。

如果現在我查看序列化程序,它會返回給我

{
    "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
            }
        }
    ]
}

我們可以看到幾何,它總是具有相同的值

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

看起來“位置”字段沒有更新。 為什么? 可能是,我不是很了解這里的這部分

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

何時執行保存/更新? 我應該運行./manager.py makemigrations./manager.py migrate來更新“位置”字段嗎?

非常感謝!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM