简体   繁体   中英

how to select data between 2 dates using sql queries in django?

models.py

My 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)

Using user_roles model, I have assigned permission for accessing data to the newly created user based on his/her role. I want to get the created deals which are added by the users having role_id = 2 and deals created date between the specified dates .

### 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()

While executing the query it shows unsupported format error. So I used one more % symbol in the same query as follows:

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))

It doesn't give any error but the result is empty even though there is data because of this syntax: DATE_FORMAT(closed_date,'%%Y-%%m-%%d') . How to solve this?

First of all you should use ForeignKey fields for role_id in Custom_user_model and user_id in Deal . The same is probably true for some of the other _id fields in your models.

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

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

After that you can do your query like this:

# 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM