2

这个解决起来很有趣。我正在构建一个模块来注册医院、医疗商店和医生的地址。有一个抽象模型 PrimaryAddress 和一个名为 MedicalStorePrimaryAddress 的子类,更多的子类将使用相同的抽象模型。我正在使用 django rest 框架来获取基于邻近度(纬度、经度和城市)的列表。现在我如何使用父类过滤它,即 PrimaryAddress 模型,因为我想过滤所有实体,即附近的医院、医疗商店和医生。我研究了 django-polymorphic 库,但它对 geodjango 和抽象类没有帮助。任何帮助建议表示赞赏。谢谢 这是代码示例:

#  MODELS

class PrimaryAddress(gismodels.Model):
    street = gismodels.CharField(max_length=255)
    city = gismodels.CharField(max_length=60)
    state = gismodels.CharField(max_length=100,
                                choices=settings.US_STATES,
                                default="CT")
    landmark = gismodels.TextField()
    latitude = gismodels.FloatField(null=True, blank=True)
    longitude = gismodels.FloatField(null=True, blank=True)
    location = gismodels.PointField(null=True, blank=True)

    objects = gismodels.GeoManager()

    def __unicode__(self):
        return self.street

    class Meta:
        verbose_name = "Address"
        verbose_name_plural = "Addresses"
        abstract = True

    def save(self, *args, **kwargs):
        if self.latitude and self.longitude:
            self.location = Point(self.longitude, self.latitude)

        super(PrimaryAddress, self).save(*args, **kwargs)


class MedicalStoreAddress(PrimaryAddress):
    medical_store = gismodels.OneToOneField(MedicalStore, related_name="medical_store_address",
                                            on_delete=gismodels.CASCADE, null=True, blank=True)

    # objects = gismodels.GeoManager()

    def __unicode__(self):
        return self.street

    class Meta:
        verbose_name = "Medical Store Address"
        verbose_name_plural = "Medical Store Addresses"

    def save(self, *args, **kwargs):
        if self.latitude and self.longitude:
            self.location = Point(self.longitude, self.latitude)

        super(MedicalStoreAddress, self).save(*args, **kwargs)

# VIEW
class ProximityFilter(ListAPIView):
    serializer_class = AddressSerializer
    # authentication_classes = (authentication.TokenAuthentication, authentication.SessionAuthentication,)
    # permission_classes = (permissions.IsAuthenticated,)
    pagination_class = StandardResultsSetPagination

    def get_queryset(self):
        longitude = self.kwargs.get('longitude')
        latitude = self.kwargs.get('latitude')
        city = self.kwargs.get('city')
        current_point = GEOSGeometry('POINT(%s %s)' % (longitude, latitude), srid=4326)
        # raise
        queryset = MedicalStoreAddress.objects.filter(city__iexact=city, location__distance_lte=(current_point, D(mi=700000000))).distance(
            current_point).order_by('distance')
        return queryset

# SERIALIZER

class AddressSerializer(HyperlinkedModelSerializer):


    class Meta:
        model = DoctorPrimaryAddress
        fields = ('pk', 'street', 'latitude', 'longitude', 'city')

This paste expires on 2018-03-29 21:26:23. View raw. Remove now (Why am I seeing this?) Pasted through web.
4

0 回答 0