@royalkaden (やすなが)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Django

Djangoのユーザー定義関数の使い方

現在、友人を管理するシステムを作っています。
友人のオブジェクトには誕生日と年齢があり、誕生日を迎えたら年齢を自動で加算する関数を作りました。しかしその関数をどのFileにおきどのように記載すれば実行できるのかわかりません。教えていただけますでしょうか。

該当するソースコード

models.py
@classmethod
class humanList(models.Model):

    #名前
    name = models.CharField(
        max_length= 30,
    )

    #年齢
    age = models.IntegerField(

    )
    def born_years(self,age):
        import datetime
        today = datetime.date.today()
        sdate = today.strftime("%Y")
        date_year = int(sdate)
        return date_year - self.age



    #アルバイト先
    part = models.CharField(
        max_length = 15,
        null= True,
        blank=True,
        default="なし"
    )
    #勤め先
    belongs = models.CharField(
        max_length = 20,
        null= True,
        blank=True,
        default="なし"
    )   
    #出会い
    encounter = models.CharField(
        max_length = 10,
        choices = CATEGORY
    )
    #最寄り駅
    station = models.CharField(
        max_length = 15,
        null= True,
        blank=True,
        default="まだわからない"
    )


    month = models.IntegerField(
        choices = YEAER_MONTH,
        default=12
    )

    days = models.IntegerField(
        default=12
    )

    #好きなもの
    like = models.CharField(
        max_length = 20,
        null= True,
        blank=True,
        default="まだわからない"
    )
    #嫌いなもの
    hate = models.CharField(
        max_length = 20,
        null= True,
        blank=True,
        default="まだわからない"
    )
    #エピソード
    ep = models.TextField(
        max_length= 50,
        null= True,
        blank=True
    )
    def __str__(self):
        return self.name

    def age_plus(cls,month,days,age):
        now = datetime.datetime.now()
        ex_today = now.day
        ex_month = now.month
        if month == ex_today and days == ex_month:
           return age+1

自分で試したこと

理想としては毎日0時0分にmodelのhumanListクラスで定義したage_plus関数を実行したいと考えています。schedulを使ってやってみましたができませんでした。

models.py
schedule.every().day.at("00:00").do(age_plus,month,days,age)

while True:
    schedule.run_pending()
    sleep(3)

0 likes

4Answer

Comments

  1. @royalkaden

    Questioner

    この間もありがとうございました。今回もありがとうございます。この記事を参考に頑張ります。
    今この記事参考にやっていたのですが、crontabとはリナックスosのみでしかつかえないですよね?自分は今windowsを使っているのですが
  2. @royalkaden

    Questioner

    ありがとうございます!がんばります

誕生日さえ管理していれば年齢は分かるので、「誕生日と年齢」を管理する必要がそもそもないように思います。
誕生日のみ管理し、年齢はインスタンスメソッド(下で言うdef age(self):)で導出すれば良いだけではないでしょうか。

from django.utils import timezone

class humanList(models.Model):

     birthday = models.DateField('誕生日', ...)

    ...
    def age(self):
        today = timezone.now().date()
        days_diff = (self.birthday - today).days
        return int(days_diff / 365)

また余談ですが、先頭の@classmethodは不要ではないでしょうか・・・。

1Like

This answer has been deleted for violation of our Terms of Service.

This answer has been deleted for violation of our Terms of Service.

Your answer might help someone💌