Django -设置时间记录应用程序的最佳模型方式?

huangapple go评论65阅读模式
英文:

Django - best way to set up models for timeclock app?

问题

我正在开发一个员工考勤应用程序,主要是作为一个测试项目,使用Django。我是一个新手,不太确定如何最好地设置模型等。

我可以创建打卡(clockin)和下班(clockout)模型,并将用户作为外键引用。但是,我不确定如何最有效地计算同一天内多次打卡和下班事件之间的时间。

对于如何最有效地处理这个问题,有任何想法吗?

谢谢您的任何建议。

英文:

I am working on an employee timeclock app in Django mostly as a test project. I am a newbie and I am not sure how best to set up the models, etc.

I could create clockin and clockout models and reference the user as a foreign key. However then I am not sure the best way to calculate time between multiple clock-in and clock-out events in one day.

Any ideas about the best way to handle this most efficiently?

Thank you for any suggestions.

答案1

得分: 1

模型结构取决于您想要为您的应用添加哪些功能。正如您提到的,可以为签到和签退使用不同的模型,也可以像这样使用单独的模型:

class WorkVisit(models.Model):
    worker = models.ForeginKey(User, ....)
    check_in = models.DateTimeField(null=True)
    check_out = models.DateTimeField(null=True)

如果您想要计算差异,您可以这样做:

difference = worker.check_in - worker.check_out

您可以将其定义为模型属性:

class WorkVisit(models.Model):
    worker = models.ForeginKey(User, ....)
    check_in = models.DateTimeField(null=True)
    check_out = models.DateTimeField(null=True)

    @property
    def visit_duration(self):
        if not self.check_in or not self.check_out:
            return None
        return self.check_out - self.check_in
英文:

model structure depends on what functional do you want to add to your app.
It can be different models for check-in and check-out as you mentioned, it can be solo model like this:

class WorkVisit(models.Model):
    worker = models.ForeginKey(User, ....)
    check_in = models.DateTimeField(null=True)
    check_out = models.DateTimeField(null=True)

if you want to calculate difference you can do just:

difference = worker.check_in - worker.check_out

you can make it model property:

class WorkVisit(models.Model):
    worker = models.ForeginKey(User, ....)
    check_in = models.DateTimeField(null=True)
    check_out = models.DateTimeField(null=True)

    @property
    def visit_duration(self):
        if not self.check_in or not self.check_out:
            return None
        return self.check_out - self.check_in

huangapple
  • 本文由 发表于 2023年8月4日 02:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定