Django --ForeignKey() 使用与行 ID 不同的列?
我有一个名为“位置”的应用程序。 位置有国家、州、城市、附近的大城市、经度、纬度。
我有一份出售商品的申请。 项目有标题、链接、描述、价格和信息。 位置是一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 theLocation
query entirely.Honestly, I'm having trouble figuring out if that's all you need, but that handles the O(N) queries issue.