반경 100 마일 이내의 특정 tag
인 Records
을 찾고 싶습니다. 독립적으로 작동하는 두 가지 쿼리가 있지만 (아래 참조) 나는 이들을 조합하는 방법을 모릅니다.django - 하나에 여러 쿼리
Records
모델에는 geo_location
이라는 GeoLocation
모델을 가리키는 외래 키가 있습니다. 두 모델 (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)
아이디어가 있으십니까? 내가 django-taggit를 사용하고있어 Records
모델의 tags
필드에서
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)
:
이들은
내 모델입니다.
1. 실제 모델을 제시해주십시오. 2. 왜'tags__slug = tag'가 아닌 단일 요소 목록으로'in'을 사용하고 있습니까? 3. 'GeoLocation'객체가 관련 '레코드'를 미리 가져 오지 않았 음을 어떻게 알 수 있습니까? –
@ Daniel Roseman 1. 위 모델을 참조하십시오. 2. 태그 필드를 위해 djago-taggit을 사용하여 태그를 검색하기 때문입니다. 3. 템플릿 {geo_search %} {{geo.lat}} {{geo.title}} {% endfor %}의 {%에 대한 테스트}와 같은 템플릿에서 테스트했습니다. {{geo.lat}}의 콘텐츠는 볼 수 있지만 {{geo.title}}은 볼 수 없습니다. – avatar