Python Forum
How to iterate a PlainLocationField object and fetch some values?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to iterate a PlainLocationField object and fetch some values?
#1
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?
Reply
#2
Return the location object to see what it is. It's type and so on.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(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
Reply
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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.
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(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.
Reply
#8
So split it by the comma and so on...
See the prev. post
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
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'
Reply
#10
  @property
  def longitude(self):
      if not self.location:
          return

      return self.location.split(',')[1]
And then
class_instance.longitude
?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing out incidence values for Class Object SquderDragon 3 268 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 14,940 Jan-19-2022, 08:33 AM
Last Post: menator01
  Object already contains values when created Stef 4 2,375 Aug-20-2020, 09:42 AM
Last Post: Stef
  How does a single object see all the values inside it? jenniferruurs 1 1,818 Oct-01-2019, 04:57 PM
Last Post: stullis
  Unable to fetch Outlook Mail body pramodb35 0 4,144 Jul-25-2018, 07:08 AM
Last Post: pramodb35
  Regular expression to fetch comments in C using Python pikkip 4 4,892 Jan-24-2017, 09:21 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020