Django筛选器中此查询的等效项是什么

时间:2019-11-22 11:59:56

标签: django django-views

此查询在Django过滤器中的等效项是什么

我需要获取今天有生日的人的电子邮件地址。

select email from lp7ms_coworker_data where Extract(month from dob)=EXTRACT(month FROM CURRENT_DATE) and Extract(day from dob)=EXTRACT(day FROM CURRENT_DATE)
class CoWorker_Data(models.Model):
    name = models.CharField('Name', max_length=50, help_text='Co-worker name.')
    email = models.EmailField('Email', help_text='Co-worker email.')
    address = models.TextField('Address', help_text='Co-worker address.')
    phone = models.CharField('Phone Number', max_length=20, help_text='Co-worker phone number.')
    companyName = models.CharField('Company Name', max_length=80, help_text='Co-worker company name.', null=True,
                                   blank=True)
    workingLocation = models.CharField('Working Location', max_length=50,
                                       help_text='Co-worker working '
                                                 'location.')
    workingShift = models.CharField('Working Shift', max_length=50, help_text='Co-worker working shift.', default='')
    workingSpace = models.CharField('Working Space', max_length=50, help_text='Co-worker working space.', default='')
    teamMembers = models.CharField('Team Members', max_length=15, help_text="Co-Worker's Team Size.", default='')
    coworkerPicture = models.ImageField('Co-Worker Picture', upload_to='../media/images/co-woker-pictures'
                                        , help_text='Co-worker Picture.', default='', null=True, blank=True)
    joiningDate = models.DateField('Joining Date', help_text='Joining Date of Co-worker',
                                   default=datetime.datetime.today, )
    dob = models.DateField('Date of Birth', help_text='Date of Birth of Co-worker',
                           default=datetime.date.today, )


2 个答案:

答案 0 :(得分:0)

您可以使用values或values_list来返回用户电子邮件。

import datetime

today = datetime.datetime.today().date()

CoWorker_Data.objects.filter(dob__day=today.day, dob__month=today.month).values('email')

答案 1 :(得分:0)

import datetime
from django.db.models import Q
cur_day = datetime.datetime.now()

q = CoWorker_Data.objects.filter(Q(dob__day=cur_day.day) & Q(dob__month=cur_day.month)).values('email')