简体   繁体   中英

Creating a request to the django model

I have a model

class paidparking(models.Model):
    adress = models.ForeignKey(Parking, on_delete=models.SET_NULL, null=True, verbose_name='Адрес парковки')
    carnumber = models.CharField(max_length=150,verbose_name='Номер автомобиля')
    amountoftime = models.IntegerField(verbose_name='Количество времени')
    price = models.FloatField(verbose_name='Цена')
    telephone = models.CharField(max_length=20,verbose_name='Номер телефона')
    email = models.EmailField(verbose_name='Электронный адрес',null=True,blank=True )
    datetimepaidparking = models.DateTimeField(auto_now_add=True, verbose_name='Дата и время оплаты')
    expirationdate = models.DateField(null=True,verbose_name='Дата начала срока действия')
    expirationtime = models.TimeField(null=True,verbose_name='Время начала срока действия')
    enddateandtime = models.DateTimeField(null=True,blank=True,verbose_name='Окончание срока действия')

The form has 2 fields with date selection

在此处输入图像描述

In the function, I get the value from these fields

 startdate = datetime.strptime(request.POST['startdate'], '%d.%m.%Y')
 enddate = datetime.strptime(request.POST['enddate'], '%d.%m.%Y')

In the model, the expirationdate field. The date in this field is in the format %d.%m.%Y

I need to form a request to the django model. If the date in the expirationdate field falls between startdate and enddate , then all records must be retrieved

Use the __range operator:

paidparking.objects.filter( expirationdate__range=(startdate, enddate))

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