使用 GeoDjango 模型作为抽象类

发布于 2024-12-11 19:16:38 字数 1644 浏览 0 评论 0原文

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

但我收到错误:

“加入字段'您是否将“距离”拼错了? 查找类型?”

我想我把模型的“对象”属性弄乱了,但我不确定。有什么想法吗? 提前致谢。

I'm playing with GeoDjango and have some doubts. I'll really appreciate any comment and suggestion.

This is my problem. First, I've defined this (abstract) class:

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

Second, I've defined this other 'Entity' class, which
represents a person or organization related to my site:

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)
    ... 

Finally, I've created a class from the previous ones:

import LocatableModel
import Entity

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

In my views, I'd like to find organizations near a specific point:

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)

But I receive the error:

"Join on field 'point' not permitted. Did you misspell 'distance' for
the lookup type?"

I think I'm doing a mess with the model 'objects' property, but I'm not sure. Any ideas?
Thanks in advance.

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

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

发布评论

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

评论(1

别在捏我脸啦 2024-12-18 19:16:38

这个错误以前曾出现过,并声称在 3 年前的这张票中已得到解决:

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

当我遇到同样的问题时,我在票证中注意到查询管理器在继承的模型中被显式设置为 GeoManager。因此,添加一行,例如


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

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

This error has been seen before, and claimed to be solved in this ticket 3 years ago:

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

When I ran into this same problem, I noticed in the ticket that the query manager was set explicitly to GeoManager in the inherited model(s). So adding a line like,


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

...may solve the issue you're seeing, it worked for me.

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