开发者

Subtract django.db.models.DateField from python's datetime.date to get age

开发者 https://www.devze.com 2023-02-06 02:17 出处:网络
In d开发者_运维问答jango, I want to get the age (in days) of an instance of a class. I tried doing that by subtracting its creation date field from today, but it does not seem to work properly. date.t

In d开发者_运维问答jango, I want to get the age (in days) of an instance of a class. I tried doing that by subtracting its creation date field from today, but it does not seem to work properly. date.today() works fine, but DateField is giving me trouble. I looked at its source code and the django docs online for my version but I'm not sure how to manipulate it to perform the subtraction.

import datetime.date
from django.db import models

class MyItem(models.Model):
    item_name = models.CharField(max_length = 30)
    creation_date = models.DateField()

    def age(self):
        return date.today() - creation_date

my_first_item = MyItem(item_name = 'First', creation_date = '2005-11-01')

print my_first_item.age.days

Any input would be greatly appreciated!


Your problem is that you are trying to use a field instance outside of a model to represent a value.

models.DateField is a class which represents a database field with a type of "date". I suspect that you are looking to do one of the following:

  1. Just do straight date math
  2. Work with a value returned by a model

In the case of 1, you don't want to use Django's models at all. All you need and want is python's date and time handling classes. For your specific example all you need to use is a pair of date objects and you will end up with a timedelta object.

To do what you were trying to do in your example with the python standard classes, see the example below:

from datetime import date

birthday = date(year=2005, month=11, day=1)
today = date.today()

age = today - birthday

print age.days()

Here we instantiate a date with the birthdate values, we get a date with today's values, subtract them to get a timedelta, and finally print the number of days between the two dates.

In the case of 2, let's look at an example model:

class Person(models.Model):
     name = models.CharField(max_length=100)
     birthday = models.DateField()

Here we have a model where we've used models.CharField and models.DateField to describe a table in the database which contains a "varchar" column and a "date" column. When we fetch instances of this model using the ORM, Django handles converting whatever value the database returns to a native datatype. Now let's look at some code that figures out the age of an instance of a person:

from datetime import date
from myapp.models import Person

person = Person.objects.get(id=1)

age = date.today() - person.birthday

print age.days

Here you can see that we fetch an instance of the person model from the database and then we subtract their birthday from today. We're able to do this here, because when we access "person.birthday" Django is transforming whatever value the database returned into a python date object. This is the same type as the date object returned by "date.today()" so the "-" operator makes sense. The result of the subtraction operation is a timedelta object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号