在django中建模以存储特定年,月和周的员工的工作时间

时间:2014-12-10 06:22:03

标签: mysql sql-server django

我正在为在线考勤系统创建一个Django应用程序,该系统必须存储员工每周工作时间几年(5或6)

我想创建一个表,其中包含employeeid,年,月,周和小时(2015年1月对于身份为200的员工,为第1周的示例4小时)

我尝试使用很少的ManyToManyFields和Foreignkey字段创建模型(年,月,周,记录),但最终创建了链接年份和employeeid,employeeid和月,年和月的表,但没有创建带有employeeid字段的表,周,月和年。

有人能告诉我在模型中使用的字段之间的关系,以便为此创建数据库。 Thanx提前

这是我的Models.py -

from django.db import models

class Month(models.Model):
    id = models.IntegerField(primary_key = True, null = False)
    weeks = models.ManyToManyField(Week, null = True)

class Year(models.Model):
    id = models.IntegerField(primary_key = True, null = False)
    month = models.ManyToManyField(Month, null = True)



class UserId(models.Model):
    id = models.IntegerField(primary_key = True, null = False)
    name = models.CharField(max_length = 56) // employee name
    year = models.ForeignKey(Year, null = True)



class Week(models.Model):
    id = models.IntegerField(primary_key =True, null = False)
    working_hours = models.IntegerField(null = True, default = 0)


class Record(models.Model):
    id = models.IntegerField(primary_key = True, null = False)
    month = models.ManyToManyField(Month, null = True)
    year = models.ManyToManyField(Year, null = True)
    week = models.ManyToManyField(Week, null=True)
    userid = models.ForeignKey(UserId, null = True)                                 

1 个答案:

答案 0 :(得分:1)

此型号将满足您的要求

注意:您不必创建主键(id)字段,它将由django自动创建。

from django.db import models


class Employee(models.Model):
    name = models.CharField(max_length = 56)


class Record(models.Model):
    employee = models.ForeignKey(Employee)
    month = models.IntegerField()
    year = models.IntegerField()
    week = models.IntegerField()
    hours = models.IntegerField()

    class Meta:
        unique_together = (('employee', 'month', 'year', 'week'),)