如何在django中使用sql查询在两个日期之间选择数据?

时间:2016-03-04 07:17:54

标签: python mysql django

models.py

我的models.py

class Custom_user_model(User):

    daily_target = models.IntegerField()
    monthly_target = models.IntegerField()
    yearly_target = models.IntegerField()
    weekly_target = models.IntegerField()
    call_target = models.IntegerField()
    email_target = models.IntegerField()
    meeting_target = models.IntegerField()
    added_under = models.IntegerField()
    profile_pic = models.TextField()
    doj = models.DateTimeField(default='')
    location_id = models.IntegerField()
    locked = models.BooleanField()
    default_currency = models.IntegerField()
    date_change_permission = models.BooleanField()
    deal_back_log = models.BooleanField()
    created_date=models.DateTimeField(auto_now_add=True)
    role_id=models.IntegerField()
    profile_pic = models.FileField(upload_to='.')
    objects = UserManager()

//This custom_user model is the extension of django's default user model.

class Deal(models.Model):

    a_choices = ((0,'yes'),(1,'no'))
    approved = models.IntegerField(choices=a_choices,default=1)
    user_id = models.IntegerField()
    company_id = models.IntegerField()
    contact_id = models.IntegerField()
    deal_title=models.CharField(max_length=200)
    deal_value = models.CharField(max_length=20)
    currency_id = models.IntegerField()
    process_id = models.IntegerField()
    expected_close_date = models.DateField(default='')
    closed_date = models.DateField()
    deal_milestone=models.IntegerField()
    created=models.DateTimeField(auto_now_add=True)
    last_modified=models.DateTimeField(auto_now_add=True)
    s_choices = ((0,'active'),(1,'won'),(2,'junk'),(3,'lost'))
    status = models.IntegerField(choices=a_choices,default=0)
    type = models.CharField(max_length=50, default='deal')
    source = models.CharField(max_length=50,default='O')

class user_Roles(models.Model):
    code = models.CharField(max_length=20)
    description = models.CharField(max_length=30)
    permitted_menus = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True)

使用user_roles模型,我已经根据他/她的角色分配了访问新创建用户的数据的权限。我想获得由role_id = 2的用户添加的已创建交易,并在指定日期之间交易创建日期。

### views.py

    st_date, end_date = week_magic(date.today())
    cur = connection.cursor()
    cur.execute("select *, CONCAT(au.first_name,' ',au.last_name) as full_name from myapp_custom_user_model mu left join auth_user au on mu.user_ptr_id = au.id INNER JOIN myapp_user_roles ml on ml.id= 2 and ml.id = mu.role_id LEFT JOIN (SELECT user_id,SUM( deal_value ) AS cnt FROM myapp_deal where status = 1 and DATE_FORMAT(closed_date,'%Y-%m-%d') BETWEEN " '%s' " and " '%s' " GROUP BY user_id)d ON mu.user_ptr_id = d.user_id where mu.locked !=1 and mu.role_id = 2 order by COALESCE( d.cnt, 0 ) DESC",(st_date,end_date))
    users = dictfetchall(cur)
    cur.close()

执行查询时,它显示不支持的格式错误。所以我在同一个查询中再使用了一个%符号,如下所示:

cur.execute("select *, CONCAT(au.first_name,' ',au.last_name) as full_name from myapp_custom_user_model mu left join auth_user au on mu.user_ptr_id = au.id INNER JOIN myapp_user_roles ml on ml.id= 2 and ml.id = mu.role_id LEFT JOIN (SELECT user_id,SUM( deal_value ) AS cnt FROM myapp_deal where status = 1 and DATE_FORMAT(closed_date,'%%Y-%%m-%%d') BETWEEN " '%s' " and " '%s' " GROUP BY user_id)d ON mu.user_ptr_id = d.user_id where mu.locked !=1 and mu.role_id = 2 order by COALESCE( d.cnt, 0 ) DESC" %(st_date,end_date))

它没有给出任何错误,但由于此语法,结果为空,即使存在数据:DATE_FORMAT(closed_date,'%%Y-%%m-%%d')。怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

首先,您应该使用role_idCustom_user_model的{​​{3}}字段和user_id中的Deal字段。对于模型中的某些其他_id字段,情况可能也是如此。

class Custom_user_model(User):
    ...
    role = models.ForeignKey('Role')
    ...

class Deal(models.Model):
    ...
    user = models.ForeignKey('Custom_user_model')
    ...

之后你就可以这样查询:

# get deals from users with role_id=2
query = Deal.objects.filter(user__role_id=2)
# add filter for deals created by that user created between    
start_date, end_date = week_magic(date.today())
query = query.filter(created__between=(start_date, end_date))