我有一个项目模型。该项目包含人员(正在从事该项目的人员)。我也试图为每个项目人员制作一个模型,包括他们在项目中的任何注释和项目完成百分比。
我的问题是我想将individual_person_in_project过滤到相应项目中的人员。我正在尝试使用
limit_choices_to = {'person_in_project':User}
我想将我的选择限制在Project模型中的用户身上。
class Project(models.Model):
project_name = models.CharField(max_length = 120,null = False,blank = False)
project_percent_complete = models.IntegerField(blank = True,null = True, default = 0)
person_in_project = models.ManyToManyField(User,related_name = 'project_person',blank = True)
project_description = models.CharField(max_length = 300,null = True,blank = True)
class Project_Person(models.Model):
corresponding_project = models.ForeignKey(Project,related_name = 'corresponding_project_this_user_is_in',null = False)
individual_person_in_project = models.ForeignKey(User, related_name = 'a_person_within_the_corresponding_project', limit_choices_to = {'person_in_project':User})
percent_complete = models.IntegerField(default = 0)
答案 0 :(得分:0)
我在上面留下了评论,但我认为这是一个更好的答案,无论如何:
您可以使用the through option跟踪manytomanyfield上的额外信息,以便获得:
class Project(models.Model):
...
person_in_project = models.ManyToManyField(User, related_name='project_person', blank=True, through=ProjectPerson)
文档解释了其余的细节,但在这种情况下你不应该处理limit_choices_to。
答案 1 :(得分:0)
感谢您的帮助,这非常有用。最有用的评论是ryanmrubin和使用ManyToManyField来促进他们之间的关系我最终创建了一个单独的类并将其与项目相关联。
如果我需要将更多信息绑定到这个新类中,我肯定会使用ManyToManyField。
class Project(models.Model):
project_name = models.CharField(max_length = 120,null = False,blank = False)
project_percent_complete = models.IntegerField(blank = True,null = True, default = 0)
project_description = models.CharField(max_length = 300,null = True,blank = True)
people_in_project = models.ManyToManyField(User,blank = True)
class Project_Tasks(models.Model):
description = models.CharField(max_length = 120,blank = True)
percent_complete = models.IntegerField(default = 0)
user = models.OneToOneField(User,unique = True,blank = True, null = True)
project = models.OneToOneField(Project,unique = True, blank = False, null = True)