0

I have the following Django models.py:

class members(models.Model):

    def calculate_age(dob):
         return int((datetime.date.today() - dob).days / 365.25  )

    auto_id = models.AutoField(primary_key=True)
    member_name = models.CharField(max_length=25, blank=False, default='')
    member_dob = models.DateField(blank=True)
    wife_name = models.CharField(max_length=25, blank=False, default='')
    wife_dob = models.DateField(blank=True)
    member_age = property(calculate_age(member_dob))
    wife_age = property(calculate_age(wife_dob))

Now from calculate_age if I pass the member_dob it is going as datefield type and not the actual value.

But If I use the self then it works fine as below,

class members(models.Model):

   def calculate_age(self):
        return int((datetime.date.today() - self.member_dob).days / 365.25  )

    def calculate_age_wife(self):
        return int((datetime.date.today() - self.wife_dob).days / 365.25  )

    auto_id = models.AutoField(primary_key=True)
    member_name = models.CharField(max_length=25, blank=False, default='')
    member_dob = models.DateField(blank=True)
    wife_name = models.CharField(max_length=25, blank=False, default='')
    wife_dob = models.DateField(blank=True)
    member_age = property(calculate_age)
    wife_age = property(calculate_age_wife)

Now the issue is, I need to call this calculate_age for multiple items so need to pass the value to the method

5
  • 1
    why would you want to pass dob, when you use self all the items of member will have access to the method as well as the attributes(that is your member_dob) Commented Aug 17, 2017 at 12:00
  • I have updated my question, you can see the working one ( i.e 2nd code), but I wanted to use only one method instead Commented Aug 17, 2017 at 12:08
  • Oh now I understand. But your first approach will not work because, you are passing the class attribute (member_dob), because until you have instances of members, member_dob is just a type of models.DateField and not any value yet. Its good to have one function when the functionality here is the same, but to that the only other way is to create a third function say calculate_age(dob) which actually calculates the age and from your other 2 functions one for the member and his wife, you have to call calculate_age(self.member_dob) and calculate_age(self.wife_dob). Commented Aug 17, 2017 at 12:18
  • Calling the function over it's instances will work correctly. I think you aren't trying it yet. Commented Aug 17, 2017 at 12:20
  • And since your fucntions is just a one liner, you might as well just follow the second approach ! Commented Aug 17, 2017 at 12:20

2 Answers 2

1

Update the code

class members(models.Model):

   def calculate_age(self):
       if self.member_dob:
           return int((datetime.date.today() - self.member_dob).days / 365.25  )
       elif self.wife_dob:
           return int((datetime.date.today() - self.wife_dob).days / 365.25)
       else:
           return None
Sign up to request clarification or add additional context in comments.

1 Comment

This works but anyway again we have to use multiple if statements if multiple age has to be calculated
0

First of all if you are working on python 2 you need to pass self instance variable with parameter. in python 3 we don't need to pass self instance explicitly. You can do it my passing two argument in the same method and inside method just need a check condition is that self refer to member_dob or wife_dob

 def calculate_age(self):
        if self.member_dob:
         return int((datetime.date.today()-self.member_dob).days/365.25) 
        else:
         return int((datetime.date.today()-self.wife_dob).days/365.25  ) 

2 Comments

He wants calculate_age to be a property of django model, your answer would work as method but not as an attribute of the model, everytime he wants member_age or wife_age, he has to pass the respective dob.
It was a slight mistake as i was considering as methods. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.