Geodjango 模型结合非地理模型

Geodjango model combined with non geo model

我是一名 Django 初学者,正在尝试编写一个简单的地理应用程序。 我的设置:django/geodjango + 传单。一切正常,并显示地理对象 (GeoObject)。但现在我想从另一个模型添加附加属性(“状态”)并通过传单显示它们 - 但我卡住了。

我的models.py:

class GeoObject(models.Model):                                     
    name = models.CharField(verbose_name="name", max_length=20)
    location = models.PointField(srid=4326)                    
 
class Status(models.Model):
    geoobject = models.OneToOneField(GeoObject, on_delete=models.CASCADE, primary_key=True, default=0, unique=True)
    status = models.CharField(verbose_name="Sensor", max_length=20)

我的views.py:

def GeoMapView(request): #view to display leaflet map with geo objects     
    q=Sensor.objects.all()
    context = {'q':q}
    return render(request, 'xitylytix_sensors/sensor_map.html', context)

def GeoData(request): #sending geo data                                                             
    q=GeoObject.objects.all()
    geo_data = serialize('geojson', q)
    return HttpResponse(geo_data, content_type='json')

我的urls.py

urlpatterns = [
    path('geomap/', views.GeoMapView, name='geo_map'), #display via template/leaflet map
    path('geodata/', views.GeoData, name='geo_data'), #sending geo data
]

json数据:

{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "properties": {"name": "001", "pk": "1"}, "geometry": {"type": "Point", "coordinates": [8.849315642079313, 50.07892796957105]}}, ...

我尝试使用一对一关系(参见模型),但 json 文件中的“状态”在“属性”中丢失。有人有想法吗?

我也试过:

我想要的是这个,但它不起作用,只是检索没有 GeoObject 数据的对象:

q = Status.objects.all().only('status','geoobject__name', 'geoobject__location',)

查询正常,但我只检索字典列表,我不能用它来序列化:

q = Status.objects.all().values('status','geoobject__name', 'geoobject__location',)

你为什么要这么做?您可以简单地在视图中创建对象并将数据添加到您的 GeoObject model.just 将数据列添加到您的数据库,然后在视图中您可以将特定数据添加到您的模型。

def GeoData(request):                                                            
    q=GeoObject.objects.all()
for item in queryset:
          GeoObject.objects.create(
                something = item.queryset(data)
                      )
    geo_data = serialize('geojson', q)
    return HttpResponse(geo_data, content_type='json')

如果你想访问另一个模型中的数据,你应该使用外键。

我终于找到了适合我的解决方案:

views.py:

def createleafletobject(q):
    queryset  = []

    for element in table:
        LeafObj = LeafletObject(
            name = element['name'],
            location = element['location'],
            geoobject = element['geoobject'],
            status = element['status],
        ) 
        queryset.append(LeafObj)
    return queryset 

def GeoMapView(request): #view to display leaflet map with geo objects     
    q=Sensor.objects.all()
    context = {'q':q}
    return render(request, 'xitylytix_sensors/sensor_map.html', context)

def GeoData(request): #sending geo data                                                             
    q = Status.objects.all().values('geoobject__name', 'geoobject__location', 'geoobject', 'status')
    q = createleafletobject(q)
    geo_data = serialize('geojson', q)
    return HttpResponse(geo_data, content_type='json')

所以我正在做的是通过“values()”从两个对象查询数据,接收和字典并用我自己写的“createleafletobject(table)”class转换成一个对象,我正在对其进行序列化并传递给传单。