mirror of
https://github.com/not-kennethreitz/convore.json.git
synced 2026-06-05 23:20:19 +00:00
1 line
5.8 KiB
JSON
1 line
5.8 KiB
JSON
[{"user_id": 5939, "stars": [], "topic_id": 13115, "date_created": 1300148660.0207019, "message": "@sebleier Thanks! That was exactly what I wanted!\n\n@bmelton I actually got the local zip search working last week. It is really easy (read: only a mild pain in the ass) with postGIS and GeoDjango.", "group_id": 81, "id": 352282}, {"user_id": 5939, "stars": [], "topic_id": 13115, "date_created": 1300147319.46995, "message": "Okay, so I am building an app in Django and I want to be able to search for entries in the database that are located in one of n zipcodes", "group_id": 81, "id": 352162}, {"user_id": 308, "stars": [{"date_created": 1300148031.520469, "user_id": 6396}, {"date_created": 1300150206.7439809, "user_id": 5939}, {"date_created": 1300155369.006851, "user_id": 927}, {"date_created": 1300163605.9252419, "user_id": 4581}], "topic_id": 13115, "date_created": 1300147827.190192, "message": "Post.objects.filter(postal_code__in=['90210', '06489', ...]) that should work", "group_id": 81, "id": 352209}, {"user_id": 5939, "stars": [], "topic_id": 13115, "date_created": 1300147521.344476, "message": "Now I could do something like:\n q = Q(postal_code='90210') | Q(postal_code=\"06489\") | Q(postal_code=\"54751\")\n p = Post.object.filter(q)\n\nBut I am not sure how I would go about chaining the Q objects programatically, and I REALLY don't want to use eval().", "group_id": 81, "id": 352172}, {"user_id": 4077, "stars": [], "topic_id": 13115, "date_created": 1300147829.6499829, "message": "If you're trying to find 'nearby' zip codes, I'd shell out to raw SQL and implement the Haversine formula -- Many years ago I had an implementation of it for mySQL (it was considerably non-trivial), but I wouldn't even begin to approach how you'd implement it in an ORM.", "group_id": 81, "id": 352210}, {"user_id": 4077, "stars": [], "topic_id": 13115, "date_created": 1300149402.2696581, "message": "My ignorance of postgres means that there are a ton of solved problems I am completely unaware of. Thanks for the update!", "group_id": 81, "id": 352351}, {"user_id": 5939, "stars": [], "topic_id": 13115, "date_created": 1300149620.309139, "message": "@bmelton No problem. There isn't a whole lot of information about it online, so most of the work was figuring out how everything worked together.\n\nNow I have another problem. How can I search by foreign key? My Zipcode model, which contains all of the zipcodes, is a foreign key of my Posts model. So if I wanted to search for all Posts that have a foreign key that has a post_code value of x, how would I do that?", "group_id": 81, "id": 352373}, {"user_id": 5939, "stars": [], "topic_id": 13115, "date_created": 1300150437.9570529, "message": "Nevermind, it seems that you can do things like this:\nzl = Zipcode.objects.filter(location__distance_lte=(z.location, D(mi=30)))\nPosts.objects.filter(location__in=zl)", "group_id": 81, "id": 352468}, {"user_id": 4581, "stars": [], "topic_id": 13115, "date_created": 1300165897.7487061, "message": "@andrewbadr essentially, yes.", "group_id": 81, "id": 353563}, {"user_id": 4581, "stars": [], "topic_id": 13115, "date_created": 1300164186.059917, "message": "This is not necessarily a good solution for the problem you posed, but it does address the original question: You can programmatically chain Q objects with Q.add().", "group_id": 81, "id": 353481}, {"user_id": 4581, "stars": [], "topic_id": 13115, "date_created": 1300164364.841954, "message": "You can then pass that \"q\" instance into filter. The second parameter to the Q.add() method is a connector (options include: Q.OR, Q.AND and the Q instance also has a connector attribute - see q.connector)", "group_id": 81, "id": 353492}, {"user_id": 4581, "stars": [], "topic_id": 13115, "date_created": 1300164266.858742, "message": "You can then chain another Q instance by doing this: q.add(Q(field=othervalue), Q.OR)", "group_id": 81, "id": 353485}, {"user_id": 4581, "stars": [], "topic_id": 13115, "date_created": 1300164227.5133641, "message": "Say, for example you do something like this: q = Q(field=somevalue)", "group_id": 81, "id": 353484}, {"user_id": 115, "stars": [], "topic_id": 13115, "date_created": 1300165387.4433839, "message": "is that the same as | and &?", "group_id": 81, "id": 353541}, {"user_id": 5939, "stars": [], "topic_id": 13115, "date_created": 1300250941.847172, "message": "I also discovered that you can chain them in this fashion as well:\nq1 = Q(some_value=True)\nq2 = Q(another_value=14)\nThing.objects.filter(q1|q2)", "group_id": 81, "id": 362695}, {"user_id": 5939, "stars": [], "topic_id": 13115, "date_created": 1300251045.6767349, "message": "Although i did not know about @eMyller 's solution. In fact, I didnt know that you could do |= and &=. Although, with python's object models, it shouldn't come as a surprise.", "group_id": 81, "id": 362706}, {"user_id": 8364, "stars": [], "topic_id": 13115, "date_created": 1300263577.4172339, "message": "You could also do:\nimport operator\nreduce(operator.or_, [Q(postal_code=code) for code in codes])", "group_id": 81, "id": 363715}, {"user_id": 21387, "stars": [], "topic_id": 13115, "date_created": 1300282855.2001569, "message": "yeah, i used it when implementing a conditional search feature once. looks beautiful. :D", "group_id": 81, "id": 365667}, {"user_id": 5939, "stars": [], "topic_id": 13115, "date_created": 1300322037.909802, "message": "@eMyller, I think that that is how I am going to implement the search on mine as well. you can chain n Q objects together in a way that doesn't look like an un-maintainable spaghetti mess.", "group_id": 81, "id": 370417}, {"user_id": 21387, "stars": [{"date_created": 1300252276.7620101, "user_id": 4581}], "topic_id": 13115, "date_created": 1300201357.9876089, "message": "@andrewbadr @tekromancr: you can also use the python operators to do this task instead of using Q.add; q1 &= q2 and q1 |= q2 work pretty well.", "group_id": 81, "id": 356977}] |