2016-07-31 79 views
0

我正在使用Django預約系統!我想展示今天的約會。這個原始查詢必須返回今天的記錄,但它不返回任何記錄!Django原始SQL查詢不返回今天的記錄

查詢看起來像這樣:

todays_appointments = Appointment.objects.raw(""" 
    SELECT appointment_Appointment.id, 
      loginReg_User.user_name, 
      appointment_Appointment.task, 
      appointment_Appointment.date, 
      appointment_Appointment.time, 
      appointment_Appointment.status 
    FROM appointment_Appointment 
    LEFT JOIN loginReg_User 
    ON appointment_Appointment.user_id = loginReg_User.id 
    WHERE loginReg_User.id={} 
     AND appointment_Appointment.date={} 
ORDER BY appointment_Appointment.time".format(user_id, today""")) 

此外,today看起來是這樣的:

today = datetime.date.today()

我試着在shell中使用Appointment.objects.filter(date=today)運行類似的查詢,它工作正常!

Models.py

from __future__ import unicode_literals 
from ..loginReg.models import User 
from django.db import models 
import datetime 

class AppointmentValidator(models.Manager): 
    def task_validator(self, task): 
     if len(task) < 1: 
      return False 
     else: 
      return True 

def date_validator(self, date_time): 
    present = datetime.now().date() 
    if date_time < present: 
     return False 
    else: 
     return True 
def status(self, status): 
    if status == "missed" or status == "done" or status == "pending": 
     return True 
    else: 
     return False 


class Queries(models.Manager): 
    def todays_appointments(self, user_id): 
     today = datetime.date.today() 
     todays_appointments = Appointment.objects.raw("SELECT appointment_Appointment.id, loginReg_User.user_name, appointment_Appointment.task, appointment_Appointment.date, appointment_Appointment.time, appointment_Appointment.status FROM appointment_Appointment LEFT JOIN loginReg_User ON appointment_Appointment.user_id = loginReg_User.id WHERE loginReg_User.id={} AND appointment_Appointment.date={} ORDER BY appointment_Appointment.time".format(user_id, today)) 
    return todays_appointments 

def other_appointments(self, user_id): 
    today = datetime.date.today() 
    other_appointments = `Appointment.objects.raw("SELECT   appointment_Appointment.id, loginReg_User.user_name, appointment_Appointment.task, appointment_Appointment.date, appointment_Appointment.time, appointment_Appointment.status FROM appointment_Appointment LEFT JOIN loginReg_User ON appointment_Appointment.user_id = loginReg_User.id WHERE loginReg_User.id={} AND appointment_Appointment.date>{} ORDER BY appointment_Appointment.time".format(user_id, today))` 
    return other_appointments 


class Appointment(models.Model): 
    task = models.CharField(max_length=150) 
    date = models.DateField(null=False, blank=False) 
    time = models.TimeField() 
    status = models.CharField(max_length=45) 
    user = models.ForeignKey(User) 
    created_at = models.DateTimeField(auto_now_add = True) 
    updated_at = models.DateTimeField(auto_now = True) 

    appointmentManager = AppointmentValidator() 
    queries = Queries() 
    objects = models.Manager() 
+0

請勿使用。 '.format()'在任何SQL查詢上。它容易受到SQL注入的影響。在字符串中使用'%s'並將參數作爲列表提交。示例:https://docs.djangoproject.com/en/1.9/topics/db/sql/#executing-custom-sql-directly –

+0

此外:您可以通過ORM輕鬆完成您的工作。 –

+0

有關如何使用ORM做到這一點的任何建議?我是django的新手,這就像我第一個完整的項目!欣賞它! – Ketan

回答

0

日期是將DateField所以你需要把它比作一個日期,而不是日期的格式表示(見這樣的回答:https://stackoverflow.com/a/18505739/5679413)。通過使用格式,您今天轉換爲數字和破折號。您需要將SQL查詢重新轉換爲日期。 Django大概是爲你照顧的。

+0

如果是這樣的話,那麼第二個查詢'other_appointments'不應該爲我工作!如我錯了請糾正我。 – Ketan

+0

它工作嗎?我得到'運營商不存在:日期>整數' –

+0

沒有建議的帖子沒有幫助!但使用'>'的其他查詢適用於我。不知道爲什麼它不適合你。 – Ketan