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を格納できますが、その内容を取得すると、 が返されます。LocationPointPoint

どうすればこれを達成できますか?

4

0 に答える 0