Skip to content

Instantly share code, notes, and snippets.

@fcasco
Last active May 25, 2019 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcasco/06ded37f06a3d74a6de74287e210aea0 to your computer and use it in GitHub Desktop.
Save fcasco/06ded37f06a3d74a6de74287e210aea0 to your computer and use it in GitHub Desktop.
Extra SQL en Django. 2
from django.db import models
from django.utils improt timezone
class Person(models.Model):
first_name = models.CharField(max_length=42)
last_name = models.CharField(max_length=42)
birth_date = models.DateField()
@property
def age(self):
now = timezone.now()
years = now.year - self.birth_date.year
if now.month < self.birth_date.month:
age -= 1
elif now.month == self.birth_date.month and now.day < birth_date.day:
age -= 1
return age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment