Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用django模型方法作为静态函数_Python_Django_Pandas_Class_Django Models - Fatal编程技术网

Python 使用django模型方法作为静态函数

Python 使用django模型方法作为静态函数,python,django,pandas,class,django-models,Python,Django,Pandas,Class,Django Models,我想使用django模型方法作为静态函数。更具体地说,我希望在我的table Patient实例上使用一个名为age的模型方法,如下所示,但我也希望使用pandas apply函数将age函数应用于pandas数据框中的行。这是可能的,还是我必须专门编写另一个函数来处理我的数据帧 class Patient(models.Model): pat_id1 = models.AutoField(db_column='Pat_ID1', primary_key=True) birt

我想使用django模型方法作为静态函数。更具体地说,我希望在我的table Patient实例上使用一个名为age的模型方法,如下所示,但我也希望使用pandas apply函数将age函数应用于pandas数据框中的行。这是可能的,还是我必须专门编写另一个函数来处理我的数据帧

class Patient(models.Model):
    pat_id1 = models.AutoField(db_column='Pat_ID1', primary_key=True)  
    birth_dttm = models.DateTimeField(db_column='Birth_DtTm', blank=True, null=True)

    objects = models.Manager()

    class Meta:
        managed = False
        db_table = 'Patient'

     @property
     def age(self):
         return relativedelta(date.today(), self.birth_dttm.date()).years

您的
age
功能正在使用
self
。这意味着此函数依赖于类的实例来提供输出。因此,此方法不能声明为
staticmethod

编辑 静态方法的摘录有一个非常清楚的用例。当我们需要一些功能时,不是w.r.t对象,而是w.r.t完整的类,我们会使方法成为静态的。当我们需要创建实用工具方法时,这是非常有利的,因为它们与对象生命周期无关