Django:使用邮政编码按州提取记录列表

发布于 2024-10-11 19:53:55 字数 973 浏览 7 评论 0原文

我有一个 Django 应用程序,其中包含一系列带有邮政编码标记的帖子。我想创建一个页面,按州显示所有帖子,但不知道如何去做。我确实有一个 ZipCode 表,但我的 Post.zipcode 字段与它无关(主要是因为它是用户输入的,并且允许不在数据库中或来自美国以外的邮政编码)。

我的相关模型:

class Post(models.Model):
    body = models.TextField()
    zipcode = models.CharField(max_length=5)

class ZipCode(models.Model):
    zipcode = models.CharField(max_length=5)
    city = models.CharField(max_length=64)
    statecode = models.CharField(max_length=2)
    statename = models.CharField(max_length=32)
    latitude = models.FloatField()
    longitude = models.FloatField()

在我的 Django 视图中,我喜欢采用从我的 url 模式传入的“state”参数并执行如下操作:

def posts_by_state(request, state):
    posts = Post.objects.filter(zipcode__statecode=state)
    ...

不幸的是,我的 Post.zipcode 字段不是 ZipCode 的外键,所以我得到如果我尝试,会出现此错误:

FieldError at /post/state/VT/
Join on field 'zipcode' not permitted.

有人提示我应该如何构建一个查询集,将所有帖子集中在一起以获得请求的状态吗?先感谢您。

I have a Django app that has a series of zip code tagged posts. I'd like to create a page that shows all posts by state but am not sure how to go about it. I do have a ZipCode table, but my Post.zipcode field is not related to it (mostly because it is user entered, and allows zips that are not in the DB or from outside the US).

My relevant models:

class Post(models.Model):
    body = models.TextField()
    zipcode = models.CharField(max_length=5)

class ZipCode(models.Model):
    zipcode = models.CharField(max_length=5)
    city = models.CharField(max_length=64)
    statecode = models.CharField(max_length=2)
    statename = models.CharField(max_length=32)
    latitude = models.FloatField()
    longitude = models.FloatField()

In my Django view I'd love to take the "state" parameter that is passed in from my url pattern and do something like this:

def posts_by_state(request, state):
    posts = Post.objects.filter(zipcode__statecode=state)
    ...

Unfortunately, my Post.zipcode field is not a foreign key to ZipCode so I get this error if I try:

FieldError at /post/state/VT/
Join on field 'zipcode' not permitted.

Anyone have a hint as to how I should construct a queryset that pulls all posts together for a requested state? Thank you in advance.

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

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

发布评论

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

评论(2

酷遇一生 2024-10-18 19:53:55

我建议将 Post.zipcode 更新为 ZipCodeForeignKey。如果不能,您可以像这样进行查找:

zipcodes = [zip_code.zipcode for zip_code in ZipCode.objects.filter(statecode=state)]
posts = Post.objects.filter(zipcode__in=zipcodes)

顺便说一下,ZipCode 似乎不是该模型的正确名称。也许位置会更好。

I'd suggest updating Post.zipcode to be a ForeignKey to ZipCode. If you can't you could do the lookup like this:

zipcodes = [zip_code.zipcode for zip_code in ZipCode.objects.filter(statecode=state)]
posts = Post.objects.filter(zipcode__in=zipcodes)

On a side note, ZipCode doesn't seem like the right name for that model. Perhaps Location would be better.

浅语花开 2024-10-18 19:53:55

最后相当简单的解决方案。我所做的是将一个新的外键字段添加到称为位置的帖子中,因此帖子现在看起来像这样:

class Post(models.Model):
   body = models.TextField()
   zipcode = models.CharField(max_length=5)
   location = models.ForeignKey(ZipCode, null=True, blank=True, default=None)

当我创建新帖子时,我检查输入的 zip 字符串是否与 ZipCode 数据库中的记录匹配,如果匹配,我将创建地点FK。这让我可以在我看来这样做:

def posts_by_state(request, state):
   posts = Post.objects.filter(location__statecode=state)
   ...

谢谢 Seth 和 sdolan 的帮助!

Fairly easy solution in the end. What I did was add a new foreign key field to Post called location so Post now looks like this:

class Post(models.Model):
   body = models.TextField()
   zipcode = models.CharField(max_length=5)
   location = models.ForeignKey(ZipCode, null=True, blank=True, default=None)

When I create new Posts, I check to see if the inputted zip string matches a record in the ZipCode database, and if it does I create the location FK. This then allows me to do this in my view:

def posts_by_state(request, state):
   posts = Post.objects.filter(location__statecode=state)
   ...

Thank you Seth and sdolan for your help!

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