Django --ForeignKey() 使用与行 ID 不同的列?

发布于 2024-07-20 02:47:15 字数 1562 浏览 9 评论 0原文

我有一个名为“位置”的应用程序。 位置有国家、州、城市、附近的大城市、经度、纬度。

我有一份出售商品的申请。 项目有标题、链接、描述、价格和信息。 位置是一个ForeignKey()。

现在,如果有人想查看在美国销售的所有商品,他们可以点击一个链接(假设 http: //example.com/United-States/),URL 将传入“美国”作为国家/地区,其中 state、city 和 big_city_nearby 为空。

#in view.py
class by_location(request, country, state, city, big_city_nearby)
   location = Location.objects.filter(country=country, state=state, city=city, big_city_nearby=big_city_nearby)
   items = Items.objects.filter(location__in=[location.id for loc in location])
   return render_to_response('item/by_location.html', {"items":itmes, "country":countyr, "cit":city, "nearby":big_city_nearby})

如果他们传入就没有问题 (http://example.com/United_State/California/Oakland)因为我可以继续列出奥克兰的所有项目,而无需显示找到的每个项目的位置。

问题是当选择 http://example.com/United_States/http://example.com/United_States/California/。 因为我现在必须显示每个项目的位置,因为这些项目可能来自不同的城市。

因此,当模板获取项目列表时,它只获取每个项目中的ForeignKey(Location)。 我可以放置另一个循环,其中检查每个项目并获取 Location 对象并将其与项目对象放入一个元组中。 但那样 SQL 效率会非常低。 因为我必须为找到的每个项目访问数据库才能获取实际位置对象。

有没有一种方法可以让我说ForeignKey 保存的是城市而不是数据库中位置行的ID。

为了使问题变得简单:是否有更好的方法将位置保留为应用程序,而不需要提供国家、州、城市等。 在每个项目行中。

谢谢,

VN44CA

I have an application called Location. Location has Country, State, City, Big_City_Nearby, Longitude, latitude.

I have an application for an item for sell. Item has Title, Link, Description, Price & Location which is a ForeignKey().

Now, if someone wants to see all items for sell in the US, they click on a link (let say http://example.com/United-States/) and the URL will pass in the "United States" as country with state, city and big_city_nearby empty.

#in view.py
class by_location(request, country, state, city, big_city_nearby)
   location = Location.objects.filter(country=country, state=state, city=city, big_city_nearby=big_city_nearby)
   items = Items.objects.filter(location__in=[location.id for loc in location])
   return render_to_response('item/by_location.html', {"items":itmes, "country":countyr, "cit":city, "nearby":big_city_nearby})

There is no problem if they pass in (http://example.com/United_State/California/Oakland) as I can go ahead and list all the items in Oakland without a need to display the location per items found.

The problem is when the select http://example.com/United_States/ or http://example.com/United_States/California/. As I have to now display the location of each items as the items can be from different cities.

So when the template gets the list of items, it only gets the ForeignKey(Location) in each item. I can put in place another loop, where each item is examined and the Location object is fetched and put in a tuple with the item object. But that would be very SQL inefficient. As I would have to hit the database for each item found to get the actual location object.

Is there a way where I can say the ForeignKey is holding the city instead of the Id to the location row in the database.

To make the question simple: Is there a better way of leaving the location out as an application, without a need to provide country, state, city ...etc. in each item row.

Thx,

VN44CA

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

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

发布评论

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

评论(1

尝蛊 2024-07-27 02:47:15

Item.objects.filter(...).select_lated(“位置”)。 这将连接到位置表,因此它只是一个查询。

更好的是, Item.objects.filter(location__country=country, location__state=state).select_lated("location") 并且您可以完全省略 Location 查询。

老实说,我很难确定这是否是您所需要的,但这可以解决 O(N) 查询问题。

Item.objects.filter(...).select_related("location"). This will JOIN against the locations table, so it'll just be one query.

Even better, Item.objects.filter(location__country=country, location__state=state).select_related("location") and you can omit the Location query entirely.

Honestly, I'm having trouble figuring out if that's all you need, but that handles the O(N) queries issue.

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