Как проверить, будет ли день рождения завтра в django? - PullRequest
1 голос
/ 21 апреля 2020

Я проверяю, день рождения студентов сегодня или завтра в Python. В каждом l oop день рождения - не сегодня, даже я изменил столбец DOB на сегодняшний день. Как я могу решить это? Ниже приведен код.

Views.py

def Birthday_alert():
    students= Students.objects.all()
    tmr= dt.date.today()+ dt.timedelta(days=1)
    tmr_bdays=[]
    to_bays=[]
    # verifying if date of today is working or not
    for m in students:
        if m.dob == dt.date.today():
            print(m.name)

    # putting into dictionary
    for s in students:
        if s.dob == dt.date.today():
            to_bays.append({
                'name': 's.name',
                'course': 's.course.class_name',
                'dob' : 's.dob',
                'contact': 's.parent_contact',
            })
        elif s.dob + dt.timedelta(days=1) == tmr:
            tmr_bdays.append({
                'name': 's.name',
                'course': 's.course.class_name',
                'dob' : 's.dob',
                'contact': 's.parent_contact',
            })
        else:
            for m in students:
                print("else:",m.name)
    return (tmr_bdays,to_bays)

models.py

class Students(models.Model):
    created_by = models.ForeignKey(
        User, on_delete=models.SET_NULL, default=1, null=True)
    name = models.CharField(max_length=200, null=True)
    dob = models.DateField(null=True, verbose_name='Date of Birth')
    age = models.IntegerField()
    grade_choice = (
        ('G1', 'Grade-1'),
        ('G2', 'Grade-2'),
        ('G3', 'Grade-3'),
        ('G4', 'Grade-4'),
        ('G5', 'Grade-5'),
        ('G6', 'Grade-6'),
        ('G7', 'Grade-7'),
        ('G8', 'Grade-8'),
        ('G9', 'Grade-9'),
        ('G10', 'Grade-10'),
        ('G11', 'Grade-11'),
        ('G12', 'Grade-12'),
    )
    gender_choice=(
        ('M', 'Male'),
        ('F', 'Female'),
        ('N', 'None'),
    )

    blood_choice=(
        ('O', 'O'),
        ('A-', 'A-'),
        ('B+', 'B+'),
        ('B-', 'B-'),
        ('A+', 'A+'),
        ('AB', 'AB'),
    )

    relation_choice=(
        ('Uncle', 'Uncle'),
        ('Aunty', 'Aunty'),
        ('Father', 'Father'),
        ('Mother', 'Mother'),
        ('Grandpa', 'Grandpa'),
        ('Grandma', 'Grandma'),
        ('Sister', 'Sister'),
        ('Brother', 'Brother'),
    )
    gender=models.CharField(choices=gender_choice, max_length=10, null=True)
    grade = models.CharField(choices=grade_choice, max_length=10, null=True)
    attending_school = models.CharField(max_length=100, null=True)
    course = models.ForeignKey(
        Create_Class, on_delete=models.SET_NULL, default=1, null=True)
    address = models.TextField(null=True)
    parent_name = models.CharField(max_length=200, null=True)
    parent_contact = models.CharField(max_length=20, null=True)
    parent_address = models.TextField(null=True)
    emerg_name = models.CharField(max_length=200, null=True, verbose_name='Emergency Name')
    emerg_contact = models.CharField(max_length=20, null=True, verbose_name='Emergency Contact')
    emerg_relation = models.CharField(choices=relation_choice ,max_length=20, null=True, verbose_name='Emergency Relationship')
    doc_name = models.CharField(max_length=200, null=False, default='None', verbose_name='Doctor Name')
    doc_contact = models.CharField(max_length=20, null=False, default='None', verbose_name='Doctor Contact')
    blood_type = models.CharField(choices=blood_choice,max_length=10, null=True)
    allergic = models.TextField(null=True)

Я ожидаю, что у учеников есть дни рождения сегодня и завтра! Ваша помощь будет оценена. Спасибо!

1 Ответ

4 голосов
/ 21 апреля 2020

Используйте day и month filter as,

from datetime import timedelta
from django.utils import timezone

datetime_now = timezone.now()
now_day, now_month = datetime_now.day, datetime_now.month
datetime_tomorrow = datetime_now + timedelta(days=1)
tomorrow_day, tomorrow_month = datetime_tomorrow.day, datetime_tomorrow.month

birth_day_today = Students.objects.filter(<b>dob__day=now_day, dob__month=now_month</b>)
birth_day_tomorrow = Students.objects.filter(<b>dob__day=tomorrow_day, dob__month=tomorrow_month</b>)
...