使用模型的字段为模型中的用户字段提供选择

时间:2016-06-06 09:28:22

标签: python django

我的模型中有一个CharField部门,它从一组名为部门选择的元组和两个用户assign_to和批准的外键字段中进行选择。我应该如何限制选择,使得assigned_to用户属于与deprtment相同的组,并且approval_by是超级用户以及属于与部门相同的组。

Model.py

typechoices = (("DATA",'Data'),("ERROR",'Error'),("NEW_FEATURE",'new feature'),("CHANGE",'change'))
statuschoices =(('open',"OPEN"),('wip',"WIP"),('closed',"CLOSED"),('pending',"PENDING"))
prioritychoices=(('high',"High"),('med','MED'),('low','LOW'))
departmentchoices = (('technical',"Technical"),('allocation',"Allocation"),('operations',"Operations"),('marketing',"Marketing"),('hr',"HR"))
fs = FileSystemStorage(location='media')
def get_upload_file_name(instance,filename):
  return "manage/static/manage/%s_%s"%(str(time()).replace('.','_'),filename.replace(' ','_')) 


class Request(models.Model):
      # user = models.ForeignKey(User,null=True,related_name="user")
      department = models.CharField(max_length=20,choices=departmentchoices,default='technical')
      request_type = models.CharField(max_length=20,choices=typechoices)
      priority=models.CharField(max_length=20,choices=prioritychoices)
      description = models.TextField()
      sample = models.FileField(storage=fs,blank=True,null=True)
      approval_flag = models.BooleanField(default=False)
      status = models.CharField(max_length=20,choices=statuschoices,default='pending')
      created=models.DateTimeField(auto_now_add=True,null=True)
      modified = models.DateTimeField(auto_now=True)
      assigned_to = models.ForeignKey(User,blank=True,null=True,limit_choices_to= {'groups__name' : department})
      approval_by = models.ForeignKey(User,limit_choices_to={'is_superuser': True,'groups__name' : department},related_name="apprby",blank=True,null=True)   

0 个答案:

没有答案