根据django中的URL参数进行原始sql查询

时间:2015-08-25 18:23:50

标签: python django django-rest-framework rawsql

我的models.py如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Picker Example</title>

    <script type="text/javascript">

    // The Browser API key obtained from the Google Developers Console.
    // Replace with your own Browser API key, or your own key.
    var developerKey = 'AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXM';

    // The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
    var clientId = "XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"

    // Replace with your own App ID. (Its the first number in your Client ID)
    var appId = "XXXXXXXXXXXX";

    // Scope to use to access user's Drive items.
    var scope = ['https://www.googleapis.com/auth/drive'];

    var pickerApiLoaded = false;
    var oauthToken;

    // Use the Google API Loader script to load the google.picker script.
    function loadPicker() {
      gapi.load('auth', {'callback': onAuthApiLoad});
      gapi.load('picker', {'callback': onPickerApiLoad});
    }

    function onAuthApiLoad() {
      window.gapi.auth.authorize(
          {
            'client_id': clientId,
            'scope': scope,
            'immediate': false
          },
          handleAuthResult);
    }

    function onPickerApiLoad() {
      pickerApiLoaded = true;
      createPicker();
    }

    function handleAuthResult(authResult) {
      if (authResult && !authResult.error) {
        oauthToken = authResult.access_token;
        createPicker();
      }
    }

    // Create and render a Picker object for searching images.
    function createPicker() {
      if (pickerApiLoaded && oauthToken) {
        var view = new google.picker.View(google.picker.ViewId.DOCS);
        view.setMimeTypes("image/png,image/jpeg,image/jpg");
        var picker = new google.picker.PickerBuilder()
            .enableFeature(google.picker.Feature.NAV_HIDDEN)
            .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
            .setAppId(appId)
            .setOAuthToken(oauthToken)
            .addView(view)
            .addView(new google.picker.DocsUploadView())
            .setDeveloperKey(developerKey)
            .setCallback(pickerCallback)
            .build();
         picker.setVisible(true);
      }
    }

    // A simple callback implementation.
    function pickerCallback(data) {
      if (data.action == google.picker.Action.PICKED) {
        var fileId = data.docs[0].id;
        alert('The user selected: ' + fileId);
      }
    }
    </script>
  </head>
  <body>
    <div id="result"></div>

    <!-- The Google API Loader script. -->
    <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=loadPicker"></script>
  </body>
</html>

我想要的是一个查询集,它查找过去六个月按月分组的处方数。我使用的是raw_sql,我的views.py如下:

class Prescription(models.Model):
    date_prescribed = models.DateField()
    doctor = models.ForeignKey(Doctor)  
    pharmacy = models.ForeignKey(Pharmacy)

class Doctor(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()

class Pharmacy(models.Model):
    name = models.CharField(max_length=100)
    status = models.Charfield(max_length=100)

工作得很好,我访问网址时得到的输出如下: enter image description here

目前开始和结束日期都是硬编码的,处方数量适用于所有医生和药房。但是,我现在需要根据3个参数过滤结果:

  1. 医生
  2. 药学
  3. Start_dat和End_date
  4. 我正在尝试在URL中添加这些参数,如:

    MyApp的/服务/概述医生=约翰&安培;药房= Phizer&安培;起始日期= 2015年7月28日&安培; END_DATE = 2015年2月12日

    如何根据URL参数动态捕获这些参数并更改SQL查询?

1 个答案:

答案 0 :(得分:0)

Rest框架提供$array1 = array("item1" => "orange", "item2" => "apple", "item3" => "grape"); $array2 = array("key1" => "peach", "key2" => "apple", "key3" => "plumb"); $array3 = array("val1" => "lemon"); $newArray = array_merge($array1, $array2, $array3); foreach ($newArray as $key => $value) { echo "$key - <strong>$value</strong> <br />"; } 方法。您可以覆盖它并实现您想要的新功能。

get_queryset