我需要帮助解决这个模型关系。我是Django和Python的新手
我正在努力建立一个员工绩效评估应用程序,下面是它的模型。我在模型(PerformanceAppraisal)本身解释了我的问题。我愿意接受任何建议我可以改变我的所有模型。
编辑1:将第一个模型中符合条件的员工的外键更改为ManyToManyField
from django.db import models
from employee.models import employee
# Company head will initiate this years appraisal and he will select eligible emplayees
class AppraisalCycle(models.Model):
period = models.CharField('For The Period',max_length=100)
description = models.TextField('Description',max_length=255,help_text=('If required mention Short Description for this period'),blank=True)
eligibleEmployees = models.ManyToManyField(employee, verbose_name='Eligible Employees')
created_on = models.DateTimeField('Created On',auto_now_add=True,editable=False)
updated_on = models.DateTimeField('Last Update',auto_now=True,editable=False)
def __str__(self):
return self.period
#Overall Appraisal information will be created for all eligible employees
class PerformanceAppraisal(models.Model):
"""Initially I tried to put foreign key relationship with AppraisalCycle with to_field="eligibleEmployees"
but this asked me to give unique=True condition this wont be possible as this will have duplicate when next year appraisal is proposed for an employee
I thought I can give ManyToManyField relationship but There is no to_field option so I can select eligibleEmployees
Can you please suggest what I should do here? If you think I should change my model structure Even I am ok with it"""
appraisee = #Problem Faced here cannot use ForeignKey with to_field without unique constraint can't use ManyToMany field as well reviewer = models.ForeignKey(employee, verbose_name='Reviewer')
period = models.ForeignKey(AppraisalCycle,verbose_name='For The Period',related_name="perf_period")
overallrating = models.DecimalField('Overall Rating', max_digits=2, decimal_places=1,null=True, blank=True)
created_on = models.DateTimeField('Created On',auto_now_add=True,editable=False)
updated_on = models.DateTimeField('Last Update',auto_now=True,editable=False)
class Meta:
unique_together = (("appraisee", "period"),)
def __str__(self):
appr = str(self.appraisee)
return appr
RATING_CHOICES = ((0.0,0.0), (0.5,0.5), (1.0,1.0), (1.5,1.5), (2.0,2.0), (2.5,2.5), (3.0,3.0), (3.5,3.5), (4.0,4.0), (4.5,4.5), (5.0,5.0) )
APPROVE_CHOICES = ((1, "Yes"),(0, "No"),(2,"Reject"))
#Employees will create objective and submit for approaval, the rate after the approval, Reviewer will rate then overall rating is calculated published in the above model after everything is completed
class PerfApprObjectives(models.Model):
appraisee = models.ForeignKey(PerformanceAppraisal,verbose_name='Appraisee',related_name="Perf_Appraisee")
objective = models.CharField('Objective',max_length=100,help_text=('Max 100 Characters'),)
Description = models.TextField('Roles/Responsibilities',max_length=2000,help_text=('Max 2000 Characters'),)
measurement = models.TextField('Measurement Criteria',max_length=2000,help_text=('Max 2000 Characters'),)
apprComments = models.TextField('Appraisee Comments',blank=True, max_length=3000,help_text=('Max 2000 Characters'),)
apprRating = models.DecimalField('Self Rating',max_digits=2, decimal_places=1,null=True, blank=True,choices=RATING_CHOICES)
revComments = models.TextField('Reviewer Comments',blank=True, max_length=3000,help_text=('Max 2000 Characters'),)
revRating = models.DecimalField('Reviewer Rating',max_digits=2, decimal_places=1,null=True, blank=True,choices=RATING_CHOICES)
mgmtComments = models.TextField('Management Comments',blank=True, max_length=3000,help_text=('Max 2000 Characters'),)
mgmtRating = models.DecimalField('Management Rating',max_digits=2, decimal_places=1,null=True, blank=True,choices=RATING_CHOICES)
approved = models.CharField('Approved',choices=APPROVE_CHOICES,default=0,null=True,blank=True,max_length=1)
apprSubmit = models.CharField('Appraisee Submit',choices=APPROVE_CHOICES,default=0,null=True,blank=True,max_length=1)
revSubmit = models.CharField('Reviewer Submit',choices=APPROVE_CHOICES,default=0,null=True,blank=True,max_length=1)
mgmtSubmit = models.CharField('Management Submit',choices=APPROVE_CHOICES,default=0,null=True,blank=True,max_length=1)
created_on = models.DateTimeField('Created On',auto_now_add=True,editable=False)
updated_on = models.DateTimeField('Last Update',auto_now=True,editable=False)
def __str__(self):
return self.objective