openwisp / django-rest-framework-gis

Geographic add-ons for Django REST Framework. Maintained by the OpenWISP Project.
http://openwisp.org
MIT License
1.07k stars 200 forks source link

Bug: This plugin is changing coordinates when creating new model instances. #283

Closed i0000010 closed 1 year ago

i0000010 commented 1 year ago

In my models.py:

class Address(models.Model):
    address1 = models.CharField(max_length=200)
    address2 = models.CharField(max_length=200, blank=True, default='')
    city = models.CharField(max_length=200)
    region = models.CharField(help_text='ISO 3166-2 Code. e.g.: AB for Alberta', max_length=2)
    postal_code = models.CharField(max_length=10)
    country = models.CharField(help_text='Alpha-2 Country Code. e.g.: CA for Canada', max_length=2)
    location = models.PointField(geography=True, blank=True, default=Point(0.0, 0.0), srid=4326)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

In my views.py:

class AddressViewSet(ModelViewSet):
    queryset = Address.objects.all()
    serializer_class = AddressSerializer
    filterset_class = AddressFilter

In my serializers.py:

class AddressSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(write_only=True, required=False)
    created_at = serializers.DateTimeField(required=False)
    updated_at = serializers.DateTimeField(required=False)

    class Meta:
        model = Address
        fields = '__all__'

When I write a new address to the DB with location data, the wrong location is written. For example, if I provide (60,-140) as the lat and lon parameters of my location field, what is written to my database is (60,-40). The longitude + 180 is being written to the DB, instead of just the longitude. Has anybody encountered the same issue?