Python Forum

Full Version: How to iterate a PlainLocationField object and fetch some values?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I am doing one mistake in my code but unable to solve this.

from location_field.models.plain import PlainLocationField
class Store(OwnedModel):
    address = models.TextField(default='Mumbai')
    location = PlainLocationField(based_fields=['address'], zoom=7, null=True)
	
	@property
    def latitude(location):
        if not location:
            return
        latitude = location[1]
        return latitude

    @property
    def longitude(location):
        if not location:
            return
        longitude = location[0]
        return longitude
Issue in above code is PlainLocationField object does not support indexing.
So I am unable to return latitude,longitude from location.
Can anyone please help me to solve this?
Return the location object to see what it is. It's type and so on.
(Jun-21-2018, 06:47 AM)wavic Wrote: [ -> ]Return the location object to see what it is. It's type and so on.

location object is returning latitude and longitude in the following format:
1.3762866,103.86537729999998
Is it just string/csv data?
Split it by the comma and return the first object for the one location and in the other @property method the same with the second object for the other location.
(Jun-21-2018, 07:00 AM)wavic Wrote: [ -> ]Is it just string/csv data?
Split it by the comma and return the first object for the one location and in the other @property method the same with the second object for the other location.

If you see my code, I am using following django package to fetch the location-
django location widget

So it's just not string/csv data.
I am not familiar with the Django library.
Return the type of the 'location' object to know how to process it. Obviously, it is not a list or a tuple so you can't get the first or the second index.
(Jun-21-2018, 07:27 AM)wavic Wrote: [ -> ]I am not familiar with the Django library.
Return the type of the 'location' object to know how to process it. Obviously, it is not a list or a tuple so you can't get the first or the second index.

So from the documentation, PlainLocationField stores the latitude and longitude values as plain text.
So split it by the comma and so on...
See the prev. post
I did following and it's working now-
from location_field.models.plain import PlainLocationField
class Store(OwnedModel):
  address = models.TextField(default='Mumbai')
  location = PlainLocationField(based_fields=['address'], zoom=7, null=True)

  @property
  def latitude(self):
      if not self.location:
          return
      latitude, _ = self.location.split(',')
      return latitude

  @property
  def longitude(self):
      if not self.location:
          return
      _, longitude = self.location.split(',')
      return longitude

  class Meta:
      managed = False
      db_table = 'store'
  @property
  def longitude(self):
      if not self.location:
          return

      return self.location.split(',')[1]
And then
class_instance.longitude
?