0

I have a simple model like this:

class Place(models.Model):
    location = LocationField(
        map_attrs={"style": "mapbox://styles/mightysharky/cjwgnjzr004bu1dnpw8kzxa72", "center": (17.031645, 51.106715)})
    latitude = models.FloatField(blank=True, null=True)
    longitude = models.FloatField(blank=True, null=True)

the location field gets a latitude,longitude of a selected loction using a mapbox api. when, submitted, it gets saved like this: location = (43.12,54,12) I want to split this output and save it into latitude and longitude field. how do I write a pre_save method in order to do this?

********* UPDATE ********* I wrote this pre_save signal but its not working:

@receiver(pre_save)
def pre_save_software_reciever(sender, instance, *args, **kwargs):
    instance.location = "{},{}".format(instance.latitude, instance.longitude)
pre_save.connect(pre_save_software_reciever, sender=Place)
Sam
  • 379
  • 2
  • 10
  • Maybe take a look at this question: https://stackoverflow.com/questions/6461989/populating-django-field-with-pre-save to use the pre-save signal or you can also override the save method like so: https://stackoverflow.com/questions/56338360/how-to-override-django-save-method-to-update-some-field-dynamically – Mekicha Nov 01 '21 at 07:24
  • What is the type of the location ? is that a string ? – SANGEETH SUBRAMONIAM Nov 01 '21 at 07:31
  • I think so. its like a tuple. in admin page it shows the location field like the above. either its string or float – Sam Nov 01 '21 at 07:33
  • and I know how to write a pre_save signal. but I dont know how to split that location and pre populate those lat and long fields – Sam Nov 01 '21 at 07:34
  • why not consider overriding save... if the location is a string it should be the easiest way to go.. and just to confirm the location value has two comma's ? – SANGEETH SUBRAMONIAM Nov 01 '21 at 07:36
  • location value is like this = (first_number,second_number) just like a tuple. I edited the question and added my presave method. its not working though. – Sam Nov 01 '21 at 07:39
  • how do I write a split code if I want to override the save function? I dont know how to do that – Sam Nov 01 '21 at 07:44

1 Answers1

1

1 IF Location is a SET ,

class Place(models.Model):
    location = LocationField(
        map_attrs={"style": "mapbox://styles/mightysharky/cjwgnjzr004bu1dnpw8kzxa72", "center": (17.031645, 51.106715)})
    latitude = models.FloatField(blank=True, null=True)
    longitude = models.FloatField(blank=True, null=True)


    def save(self, *args, **kwargs):    
        #Directly access tuple values
        self.latitude = self.location[0]
        self.longitude = self.location1[1]
        super(Model, self).save(*args, **kwargs)

2 If Location is a string ,

 class Place(models.Model):
    location = LocationField(
        map_attrs={"style": "mapbox://styles/mightysharky/cjwgnjzr004bu1dnpw8kzxa72", "center": (17.031645, 51.106715)})
    latitude = models.FloatField(blank=True, null=True)
    longitude = models.FloatField(blank=True, null=True)


    def save(self, *args, **kwargs):    
        #manipulate the string to extract lat and long...
        location_coordinates = self.location.split(',')
        self.latitude = float(location_coordinates[0][1:])
        self.longitude = float(location_coordinates[1][:-1])
        super(Model, self).save(*args, **kwargs)
SANGEETH SUBRAMONIAM
  • 1,098
  • 1
  • 9
  • 10