django - 将多个查询合并为一个

发布于 2024-12-07 03:44:52 字数 1663 浏览 1 评论 0原文

我想在 100 英里半径内找到具有特定 标签记录。我有两个独立工作的查询(见下文),但我不知道如何将它们组合在一起。

此外,Records 模型有一个指向名为 geo_locationGeoLocation 模型的外键。我希望能够一次性显示两个模型 (RecordsGeoLocation)中的字段。我尝试在下面的 GeoLocation 查询上使用 .select_lated() ,但由于某种原因,我只让它显示 GeoLocation 模型字段,而不是正如我所料,额外的 Records 模型字段。

tag_search = Records.objects.filter(tags__slug__in=[tag])
geo_search = GeoLocation.objects.select_related().filter(srid2163__distance_lte=(pnt, D(mi=100))).distance(pnt)

有什么想法吗?


这些是我的模型:

from taggit.managers import TaggableManager
from django.contrib.gis.db import models

class GeoLocation (models.Model):
    lat = models.FloatField(blank=True)
    long = models.FloatField(blank=True)
    srid2163 = models.PointField(blank=True,srid=2163)
    server_time = models.DateTimeField(auto_now_add=True)

    objects = models.GeoManager()

    def __unicode__(self):
        return u'%s %s %s' % (self.lat, self.long, self.server_time)


class Records(models.Model):
    title = models.CharField(blank=True, max_length=50)
    message_body = models.TextField()
    server_time = models.DateTimeField(auto_now_add=True)
    geo_location = models.ForeignKey(GeoLocation, related_name='geoloc')


    tags = TaggableManager()

    def __unicode__(self):
        return u'%s %s %s' % (self.title, self.message_body, self.server_time)

对于 Records 模型中的 tags 字段,我使用 django-taggit

I want to find the Records with a certain tag within 100 mile radius. I have two queries that work independently (see below) but I don't know how to put them together.

Also Records model has a foreign key pointing to the GeoLocation model called geo_location. I want to be able to show fields from both models (Records and GeoLocation) in one shot. I tried with .select_related() on the GeoLocation query below but for some reason I only get it to show the GeoLocation model fields and not the additional Records model fields as I expected.

tag_search = Records.objects.filter(tags__slug__in=[tag])
geo_search = GeoLocation.objects.select_related().filter(srid2163__distance_lte=(pnt, D(mi=100))).distance(pnt)

Any ideas?


These are my models:

from taggit.managers import TaggableManager
from django.contrib.gis.db import models

class GeoLocation (models.Model):
    lat = models.FloatField(blank=True)
    long = models.FloatField(blank=True)
    srid2163 = models.PointField(blank=True,srid=2163)
    server_time = models.DateTimeField(auto_now_add=True)

    objects = models.GeoManager()

    def __unicode__(self):
        return u'%s %s %s' % (self.lat, self.long, self.server_time)


class Records(models.Model):
    title = models.CharField(blank=True, max_length=50)
    message_body = models.TextField()
    server_time = models.DateTimeField(auto_now_add=True)
    geo_location = models.ForeignKey(GeoLocation, related_name='geoloc')


    tags = TaggableManager()

    def __unicode__(self):
        return u'%s %s %s' % (self.title, self.message_body, self.server_time)

For the tags field in the Records model I'm using django-taggit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2024-12-14 03:44:52

这里有两件事是错误的。

首先,您误解了 select_lated() 的作用。它不会将相关模型中的字段带入当前模型中。相反,它只是预取相关实例,因此执行 model_instance.foreignkey_field.field_on_lated_model 不会导致另一次数据库命中。

其次,您的模型与您最初所说的外键相矛盾。您说它是从 GeoLocation 到 Records,但模型定义显示事实恰恰相反。 select_lated 在这个方向上不起作用 - 考虑到您当前的模型,无法一次性查询 GeoLocation 并获取相关记录。但是,您可以查询记录并获取相关的地理位置。

(第三,您对我关于使用单元素 in 查找的评论的回答完全无关。请使用 tags_slug=tag。)

There are two things wrong here.

Firstly, you've misunderstood what select_related() does. It doesn't bring the fields from the related model into the current one. Instead, it just pre-fetches the related instance, so that doing the model_instance.foreignkey_field.field_on_related_model doesn't cause another db hit.

Secondly, your models contradict what you originally said about the foreignkey. You said it was from GeoLocation to Records, but the model definitions show that is the other way round. select_related does not work in that direction - there is no way, given your current models, to query GeoLocation and get the related Records in one go. However, you can query Records and get the related GeoLocations.

(Thirdly, your answer to my comment about the use of the single-element in lookup is completely irrelevant. Use tags_slug=tag.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文