我正在尝试使用Django rest框架将现有项目转换为RestAPI。这是我的第一个API,因此我可以随时进行学习。
到目前为止,我已经设法创建并序列化了模型,如我所见:
#Locations/models.py
class LocationInfo(models.Model):
areas = [
('210', '210'),
('769', '769'),
('300', '300')
]
gdt1_lat_lon = models.CharField(name='Latitude,Longitude',
unique=True, max_length=255, blank=False,
help_text="Enter the location's Latitude,Latitude, first when extracting from Google Maps.",
default=1)
gdt2_lat_lon = models.CharField(name='Latitude,Longitude',
unique=True, max_length=255, blank=False,
help_text="Enter the location's Latitude,Latitude, first when extracting from Google Maps.",
default=1)
uav_lat_lon = models.CharField(name='Latitude,Longitude',
unique=True, max_length=255, blank=False,
help_text="Enter the location's Latitude,Latitude, first when extracting from Google Maps.",
default=1)
area = models.CharField(
max_length=8,
choices=areas,
)
```
```python
#Locations/serializers.py
class LocationInfoSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = LocationInfo
fields = ("location's name", 'Latitude', 'Longitude', 'area', 'date_added')
```
```python
#Locations/views.py
from rest_framework import viewsets
from .serializers import LocationInfoSerializer
from .models import LocationInfo
```
class LocationInfoViewSet(viewsets.ModelViewSet):
queryset = LocationInfo.objects.all().order_by('date_added')
serializer_class = LocationInfoSerializer
```python
# Locations/urls.py
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'locations', views.LocationInfoViewSet)
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
还有我不是API项目的主脚本:
# main.py
from Project_Level.angle_condition import MeetAngleCond
from Project_Level.plot_folium import PlotOnMap
from Project_Level.utils import convert_to_xy
from three_Positions_plotting.positions_data_collecting import PositionsData
from three_Positions_plotting.dataframes import GetDataToGeoPandas
import numpy as np
def main(gdt1, gdt2, uav):
# Get the complete latitude, longitude, lists.
positions_data = PositionsData(gdt1=gdt1,
gdt2=gdt2,
uav=uav)
full_lat_lon_list, lat_list, lon_list = positions_data.calculate_list_lens()
# Get cartesian coordinates for every point.
gdt1_xy = np.asarray(convert_to_xy(gdt1))
gdt2_xy = np.asarray(convert_to_xy(gdt2))
# Get the angle for every point in f_lat_lon_list
angles_list = MeetAngleCond()
plot_angles_list = angles_list.create_angles_list(lat_lon_list=full_lat_lon_list,
arrayed_gdt1=gdt1_xy,
arrayed_gdt2=gdt2_xy,
uav_elev=uav[-1])
get_final_lists = GetDataToGeoPandas()
lat_lon_a, lat_lon_b, optimal = get_final_lists.create_gpd_and_final_lists(angles_list=plot_angles_list,
lat_list=lat_list,
lon_list=lon_list)
plot = PlotOnMap(lat_lon_a=lat_lon_a,
lat_lon_b=lat_lon_b,
optimal=optimal)
plot.create_map(mid_point=gdt1, zoom=13)
plot.plot_gdt_and_triangulation(gdt1=gdt1, gdt2=gdt2, uav=uav)
plot.plugins()
plot.auto_open(path="/home/yovel/PycharmProjects/Triangulation-Calculator/three_Positions_plotting/index.html",
html_map_file='index.html')
if __name__ == '__main__':
main(gdt1=,
gdt2=,
uav=)
现在,我要记住的是,从每个序列化数据中获取序列化数据,并将其分配给gdt1,gdt2和UAV,
据此(首先将每个位置的纬度和经度从字符串转换为浮点数,然后将其返回,我知道该怎么做,但我不知道它在代码中的位置。)并在main.py中运行该方法,
plot.auto_open将自动打开生成的HTML文件(一张大地图),我不知道是否可以在RestAPI中像这样运行它,因此,如果没有,我认为该视图只能返回HTML文件本身。
很抱歉,最终的结果是什么(您打算如何使用“ API”?这太清楚了-您是要从一个系统获取信息还是从另一个系统读取信息,或者将文件提交到python脚本中?