{"id":1659,"date":"2009-04-13T12:41:11","date_gmt":"2009-04-13T17:41:11","guid":{"rendered":"http:\/\/blogs.terrorware.com\/geoff\/?p=1659"},"modified":"2009-04-13T12:41:11","modified_gmt":"2009-04-13T17:41:11","slug":"django-querying-data-from-the-python-shell","status":"publish","type":"post","link":"https:\/\/blogs.terrorware.com\/geoff\/2009\/04\/13\/django-querying-data-from-the-python-shell\/","title":{"rendered":"Django: Querying data from the Python shell"},"content":{"rendered":"<p>I needed to get some stats for some research that we&#8217;re doing and was happy to see that you can use Django and the python shell to query testament data in a way that&#8217;s database independent.\u00c2\u00a0 It&#8217;s a little unintuitive if you&#8217;re thinking in SQL mode, but it is usable and super-helpful.\u00c2\u00a0 I wanted to share it with ya&#8217;ll in case you needed to quickly pull stats or examine info.<\/p>\n<p>Helpful reference Django docs:<\/p>\n<ul>\n<li>http:\/\/docs.djangoproject.com\/en\/dev\/topics\/db\/queries\/#making-queries<\/li>\n<li>http:\/\/docs.djangoproject.com\/en\/dev\/ref\/models\/querysets\/#queryset-api-reference<\/li>\n<\/ul>\n<p>Print the prison name, city, and state of all prisons that received a package sent by the Midwest Pages to Prisones Project from 2009-01-01 to 2009-03-22<\/p>\n<pre>geoff@btp:\/var\/www\/testament\/testament_trunk\/btp$ python manage.py shell\r\n&gt;&gt;&gt; import datetime\r\n&gt;&gt;&gt; from core.models import Prison, Package\r\n&gt;&gt;&gt; start_date = datetime.date(2009, 1, 1)\r\n&gt;&gt;&gt; end_date = datetime.date(2009, 3, 22)\r\n&gt;&gt;&gt; prisons = Prison.objects.filter(package__sent_on__range=(start_date, end_date), package__group__username__exact='mwpp').distinct()\r\n&gt;&gt;&gt; for prison in prisons:\r\n&gt;&gt;&gt;    print \"%s %s, %s\" % (prison.name, prison.city, prison.state)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I needed to get some stats for some research that we&#8217;re doing and was happy to see that you can use Django and the python shell to query testament data in a way that&#8217;s database independent.\u00c2\u00a0 It&#8217;s a little unintuitive if you&#8217;re thinking in SQL mode, but it is usable and super-helpful.\u00c2\u00a0 I wanted to&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.terrorware.com\/geoff\/2009\/04\/13\/django-querying-data-from-the-python-shell\/\">Continue reading <span class=\"screen-reader-text\">Django: Querying data from the Python shell<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,20],"tags":[723,57,724,672,722],"class_list":["post-1659","post","type-post","status-publish","format-standard","hentry","category-note-to-self","category-pages-to-prisoners","tag-django","tag-pages","tag-python","tag-snippet","tag-testament","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4wnIz-qL","_links":{"self":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/1659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/comments?post=1659"}],"version-history":[{"count":1,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/1659\/revisions"}],"predecessor-version":[{"id":1660,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/1659\/revisions\/1660"}],"wp:attachment":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/media?parent=1659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/categories?post=1659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/tags?post=1659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}