我想在100英里半徑範圍內找到Records
以及某個tag
。我有兩個獨立工作的查詢(見下文),但我不知道如何將它們放在一起。django - 將多個查詢合併爲一個
另外Records
模型有一個外鍵指向GeoLocation
模型稱爲geo_location
。我希望能夠一次性顯示來自(Records
和GeoLocation
兩個型號的字段)。我在GeoLocation
查詢下嘗試.select_related()
,但由於某種原因,我只能按照我的預期顯示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)
對於我使用django-taggit的Records
模式tags
領域。
1.請出示實際的模型。 2.爲什麼你用單元列表而不是'tags__slug = tag'來使用'in'? 3.你如何看待你的'GeoLocation'對象沒有預取相關的'Record'? –
@Daniel Roseman 1.看看上面的模型。 2.那是因爲我使用djago-taggit作爲標籤字段來搜索標籤。 3.我在我的模板中測試了{geo_search%} {{geo.lat}} {{geo.title}} {%endfor%}。我可以看到{{geo.lat}}的內容,但不能看到{{geo.title}}。 – avatar