0

我有一个django.contrib.gis.geos.point.Point子类:

from django.contrib.gis.geos import Point

class Location(Point):
    def __init__(self, *args, **kwargs):
        lat = kwargs.get('latitude')
        lng = kwargs.get('longitude')

        if lat and lng:
            super(Location, self).__init__(lng, lat)
        elif lat or lng:
            raise TypeError(u'You must declare latitude and longitude, not '
                'just one of them.')
        else:
            super(Location, self).__init__(*args, **kwargs)

        self.__class__ = Location

    def __unicode__(self):
        c = self.coordinates()
        return u'Location <Lat: %.5f, Lng: %.5f>' % (c['latitude'],
            c['longitude']) 

    def __str__(self):
        return unicode(self).encode('utf-8')

    def coordinates(self):
        return {
            'latitude': self.coords[1], 
            'longitude': self.coords[0]
        }

请注意,与 super 相比,此类没有额外的信息。它只有额外的方法来促进其使用。

如何创建一个django.contrib.gis.db.models.fields.PointField子类来使用Location?如果我PointField直接使用,它允许我存储 a Location(因为它本质上是 a Point),但是当我检索它的内容时,它会返回 a Point

我怎样才能做到这一点?

4

0 回答 0