简体   繁体   中英

Math Operations on DateField Django

When someone makes an entry, the date and time will be recorded as expected. What I'm curious about is how to do math operations on that date. Like I want to show the date of when the object was created, and also the date 2 weeks in the future.

models.py

from django.db import models

class Checkout(models.Model):
    member_id = models.IntegerField(null = True)
    title = models.CharField(max_length = 1000)
    date_checkout = models.DateField(auto_now = True)
    # expected_return = date_checkout * 2

I thought I had stumbled across a useful resource online that mentioned something about this but I can't find it anymore. If you could just point me to some resources online that would be awesome.

you can provide a callable as a default and then store that result (assuming you actually want to store the expected_return (ie for queries) )

class Checkout(models.Model):
    member_id = models.IntegerField(null = True)
    title = models.CharField(max_length = 1000)
    date_checkout = models.DateField(auto_now = True)
    expected_return = models.DateField(default=lambda:datetime.datetime.now()+datetime.timedelta(days=14))

Checkout.objects.filter(expeded_return__lt=datetime.datetime.now())
checked_out_item.expected_return

if you want it to be an actual field (ie it can be queried Checkout.objects.filter(expeded_return__lt=datetime.datetime.now())

if you just want an easy way to access then properties on the class can make functions look like attributes

class Checkout(models.Model):
    member_id = models.IntegerField(null = True)
    title = models.CharField(max_length = 1000)
    date_checkout = models.DateField(auto_now = True)
    @property
    def expected_return(self):
        return self.date_checkout + datetime.timedelta(days=14)

print(checked_out.expected_return)

if you go with the second option you will need to make sure you only use it AFTER the checkout date is set

from demo_app.models import Checkout
c = Checkout(member_id=1,title="asd")
c.expected_return # since there is no date_checkout we get error
Traceback (most recent call last):
  File "C:\Users\joran\AppData\Local\Programs\Python\Python37-32\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-4687ba4c4a16>", line 1, in <module>
    c.expected_return # since there is no date_checkout we get error
  File "D:\demo_django\demo_app\models.py", line 11, in expected_return
    return self.date_checkout + datetime.timedelta(days=14)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'
c.save()
c.expected_return # since there is now a checkout it works
Out[6]: datetime.date(2019, 12, 14)

import datetime
c = Checkout(member_id=1,title="asd",date_checkout=datetime.datetime.now())
c.expected_return # since we set the date_checkout it will work even though its not saved
Out[10]: datetime.datetime(2019, 12, 14, 13, 36, 21, 270897)

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