使用GeoDjango模型作为抽象类

时间:2011-10-24 19:16:05

标签: django-models geodjango

我正在玩GeoDjango而且有些疑惑。我真的很感激任何评论和建议。

这是我的问题。首先,我已经定义了这个(抽象)类:

from django.contrib.gis.db import models
from django.contrib.gis.geos import *

class LocatableModel(models.Model):
    country = models.CharField(max_length=48, blank=True)
    country_code = models.CharField(max_length=2, blank=True)
    locality = models.CharField(max_length=48, blank=True)
    sub_locality = models.CharField(max_length=48, blank=True)
    street = models.CharField(max_length=48, blank=True)
    address = models.CharField(max_length=120, blank=True)
    point = models.PointField(null=True)
    objects = models.GeoManager()

    class Meta:
        abstract = True

其次,我已经定义了另一个'Entity'类,其中 代表与我的网站相关的个人或组织:

from django.db import models

class Entity(models.Model):
    name = models.CharField(max_length=64)
    slug = models.SlugField(max_length=64, unique=True)
    website = models.URLField(verify_exists=False, blank=True)
    email = models.EmailField(blank=True)
    ... 

最后,我创建了一个以前的类:

import LocatableModel
import Entity

class Organization(Entity, LocatableModel):
    timetable = models.CharField(max_length=64)
    ... 

在我的观点中,我想找到一个特定点附近的组织:

from django.contrib.gis.geos import Point
from django.contrib.gis.measure import D

def index(request):
    pnt = Point(12.4604, 43.9420)
    dic = { 'orgs': Organization.objects.filter(point__distance__lte=(pnt, D(km=7))) }
    return render_to_response('index.html', dic)

但我收到错误:

“加入场地'点'不被允许。你拼错了'距离' 查找类型?“

我认为我对模型'对象'属性搞不定,但我不确定。有任何想法吗? 提前谢谢。

1 个答案:

答案 0 :(得分:3)

之前已经看过这个错误,并声称3年前在这张票中得到了解决:

https://code.djangoproject.com/ticket/9364

当我遇到同样的问题时,我在故障单中注意到查询管理器在继承的模型中明确设置为GeoManager。所以添加像

这样的行

class Organization(Entity, LocatableModel): 
    timetable = models.CharField(max_length=64)
    ...
    objects = models.GeoManager()

......可以解决你所看到的问题,它对我有用。