python - Django 模型保存

标签 python django django-models django-forms

如何使用模型中的函数处理工资并将其保存到year_salary

示例模型:

class Staff(models.Model):
    salary = models.FloatField()
    year_salary = ArrayField(models.FloatField()

及功能:

def function(salary):
    year_salary = salary * 12
    return year_salary

最佳答案

据我了解,您想存储一系列年薪?

class Staff(models.Model):
    salary = models.FloatField()
    year_salary = ArrayField(models.FloatField())

    def calculate_year_salary(self):
        return self.salary * 12    

    def save(self, *args, **kwargs):
        if not self.year_salary:
            self.year_salary = [self.calculate_year_salary()]
        else:
            self.year_salary.append(self.calculate_year_salary())
        super(Staff, self).save(*args, **kwargs)

关于python - Django 模型保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53762823/

相关文章:

python - 如何测试 send_file flask

python - 将命令行参数传递给Python中导入的另一个文件

django - 在 django admin 中加入两个表以获取 display_list 的列

Python-Django : ifchanged template tag

python - Django-如何确定 ModelForm init 中的字段类型?

django - 如何只保存 Django 中的特定字段?

python - Flask - 在请求之间将值存储在内存中

python - Heroku gunicorn flask 登录无法正常工作

python - 最小的工作 heroku django 应用程序

python - 如何修复错误 "' id' : Select a valid choice"on Modelformset validation?