Search code examples
djangopython-3.xdjango-modelsdjango-rest-framework

I want to add a location field in django model which take location input by putting lattitude and longitude


I also want to pick the location through Django REST API Through template..please suggest necessary django packages for it and how to write locationField in the Django Model

This is the picture that django location field exactly which I want to Add please see it


Solution

  • You can create the multiple inputs field subclassing MultiValueField:

    class Location:
        def __init__(self, latitude, longitude):
            self.latitude = latitude
            self.longitude = longitude
    
    class LocationField(MultiValueField):
        def __init__(self, *args, **kwargs):
            fields = (
                FloatField(max_value=90, min_value=-90),
                FloatField((max_value=90, min_value=-90)
            )
            super().__init__(*args, **kwargs)
    
    
        def compress(self, data_list):
            latitude, longitude = data_list
            return Location(latitude, longitude)
    

    As for the second question, sorry, but it's very unclear what you are asking for.