【问题标题】:Using GeoDjango model as an abstract class使用 GeoDjango 模型作为抽象类
【发布时间】:2011-10-24 19:16:04
【问题描述】:

我在玩 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

其次,我定义了另一个“实体”类,它 代表与我的网站相关的个人或组织:

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)

但我收到错误:

“不允许在字段 'point' 上加入。你是否拼错了 'distance' 查找类型?”

我认为我在使用模型“对象”属性时弄得一团糟,但我不确定。有任何想法吗? 提前致谢。

【问题讨论】:

    标签: django-models geodjango


    【解决方案1】:

    这个错误以前见过,并声称在3年前的这张票中解决了:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2010-11-18
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2014-12-01
      • 2018-12-15
      • 1970-01-01
      • 2018-06-16
      相关资源
      最近更新 更多