Django:使用邮政编码按州提取记录列表
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议将
Post.zipcode
更新为ZipCode
的ForeignKey
。如果不能,您可以像这样进行查找:顺便说一下,
ZipCode
似乎不是该模型的正确名称。也许位置
会更好。I'd suggest updating
Post.zipcode
to be aForeignKey
toZipCode
. If you can't you could do the lookup like this:On a side note,
ZipCode
doesn't seem like the right name for that model. PerhapsLocation
would be better.最后相当简单的解决方案。我所做的是将一个新的外键字段添加到称为位置的帖子中,因此帖子现在看起来像这样:
当我创建新帖子时,我检查输入的 zip 字符串是否与 ZipCode 数据库中的记录匹配,如果匹配,我将创建地点FK。这让我可以在我看来这样做:
谢谢 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:
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:
Thank you Seth and sdolan for your help!