【发布时间】:2022-01-25 04:41:09
【问题描述】:
我有一个 147 行的显示表。我没有对这个数据集进行任何繁重的计算,我只需要从数据库中快速获取它。目前,加载时间为 3-4 秒。其他数据来得真快,为什么? ListApiView 工作慢吗?
@permission_classes([AllowAny])
class DisplaysList(generics.ListAPIView):
queryset = Displays.objects.all()
serializer_class = serializers.DisplaySerializer
class Displays(models.Model):
name = models.CharField(max_length=45, blank=True, null=True)
owner = models.CharField(max_length=45, blank=True, null=True)
class GeoLocation(models.Model):
id = models.CharField(primary_key=True, max_length=32,
default=generate_uuid)
display = models.ForeignKey(
Displays, on_delete=models.CASCADE, blank=True, null=True)
lat = models.DecimalField(max_digits = 30, decimal_places=20, blank=True, null=True)
lon = models.DecimalField(max_digits = 30, decimal_places=20, blank=True, null=True)
我认为问题就在这里,如何有效地进行地理定位?
class DisplaySerializer(serializers.ModelSerializer):
geolocation = serializers.SerializerMethodField()
def get_geolocation(self, obj):
gl = GeoLocation.objects.filter(display = obj)
gll = list(gl.values)
return gll
class Meta:
model = Displays
fields = "__all__"
【问题讨论】:
-
请出示您的型号和序列化程序
-
试试这个:
list(GeoLocation.objects.select_related().filter(display=obj).values()) -
@Ahtisham 仍然很慢,6 秒
标签: python django django-rest-framework django-views