How to iterate a PlainLocationField object and fetch some values? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to iterate a PlainLocationField object and fetch some values? (/thread-11074.html) |
How to iterate a PlainLocationField object and fetch some values? - PrateekG - Jun-21-2018 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 longitudeIssue 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? RE: How to iterate a PlainLocationField object and fetch some values? - wavic - Jun-21-2018 Return the location object to see what it is. It's type and so on. RE: How to iterate a PlainLocationField object and fetch some values? - PrateekG - Jun-21-2018 (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 RE: How to iterate a PlainLocationField object and fetch some values? - wavic - Jun-21-2018 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. RE: How to iterate a PlainLocationField object and fetch some values? - PrateekG - Jun-21-2018 (Jun-21-2018, 07:00 AM)wavic Wrote: Is it just string/csv data? 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. RE: How to iterate a PlainLocationField object and fetch some values? - wavic - Jun-21-2018 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. RE: How to iterate a PlainLocationField object and fetch some values? - PrateekG - Jun-21-2018 (Jun-21-2018, 07:27 AM)wavic Wrote: I am not familiar with the Django library. So from the documentation, PlainLocationField stores the latitude and longitude values as plain text.
RE: How to iterate a PlainLocationField object and fetch some values? - wavic - Jun-21-2018 So split it by the comma and so on... See the prev. post RE: How to iterate a PlainLocationField object and fetch some values? - PrateekG - Jun-21-2018 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' RE: How to iterate a PlainLocationField object and fetch some values? - wavic - Jun-21-2018 @property def longitude(self): if not self.location: return return self.location.split(',')[1]And then class_instance.longitude? |