问题
first of all thanks for your help.
I'm making a form with Django which uses the OSMWidget to save coordinates (Polygons, Lines and Points) to a Geometry field in a PostgreSQL database. It works well, I can save the information in the database without any problem. And when I make a query with PgAdmin I can see the geometric fields data displayed in a Leaflet map correctly.
.
Here's some of what I have in my forms.py:
from django import forms
from django_select2 import forms as select2_forms
from django.contrib.gis import forms as osmforms
from django.forms import ModelForm
from .models import Dataset
class SessionForm(forms.ModelForm):
at_choices = [(item.title, item.title) for item in Dataset.objects.all()]
key_choices = [(item.keywords_d, item.keywords_d) for item in Dataset.objects.all()]
uuid = forms.CharField(label='', max_length=10 , widget=forms.TextInput(attrs={'class': "form-control left-half"}))
title = forms.CharField(label='Title', max_length=65536 , widget=forms.TextInput(attrs={'class': "form-control full-size-field"}))
abstract = forms.CharField(label='Abstract', max_length=65536 , widget=forms.Textarea(attrs={'class': "form-control full-size-field", 'title': 'Your name'}))
keywords_d = forms.MultipleChoiceField(label='Keywords', widget=select2_forms.Select2MultipleWidget(attrs={'class': "form-control left-half",'style': 'width:100%'}), choices=key_choices)
activity_type = forms.MultipleChoiceField(label='Activity type', widget=select2_forms.Select2MultipleWidget(attrs={'class': "form-control right-half",'style': 'width:100%'}), choices=at_choices)
related_site_we = forms.CharField(label='Related Site', max_length=256 , widget=forms.TextInput(attrs={'class': "form-control full-size-field"}))
bounding_box = osmforms.GeometryCollectionField(label='Bounding Box', widget=osmforms.OSMWidget(attrs={'class': "form-control full-size-field",'map_width': 992, 'map_height': 500}))
class Meta:
model = Dataset
fields = ['uuid','title','abstract','keywords_d','activity_type','related_site_we','bounding_box']
And this is part of the views.py:
def editor(request):
if request.method == 'GET':
if request.GET['uuid'] != '0':
session = Dataset.objects.get(uuid=request.GET['uuid'])
form = SessionForm(instance=session)
else:
form = SessionForm()
return render(request, 'form.html',
{'form': form,})
Without going into too much detail, one of the purposes of the form is to partially fill it out so that others can edit it later. When editing the form, this loads the existing data in the database for that entry, along with the coordinates we have previously entered, and this is where the problem appears, as it seems to reverse the order of latitude and longitude, appearing this way:
As I said, the coordinates are stored well, I think it's just a problem in the order of the coordinates when OSMWidget reads them. Is there any way to correct this? I've been reading documentation for hours, as well as reviewing other threads in StackOverFlow and other forums, and I can't find a solution to this.
Thanks in advance
来源:https://stackoverflow.com/questions/62287150/django-geodjango-read-coordinates-in-the-wrong-order