0

我有一个项目模型。该项目包含人员(从事该项目的人员)。我也在尝试为每个项目人员制作一个模型,包括他们对项目的任何注释和项目完成百分比。

我的问题是我想只过滤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)
4

2 回答 2

0

我在上面留下了评论,但我认为这是一个更好的答案,无论如何:

您可以使用through 选项来跟踪有关 manytomanyfield 的额外信息,因此您可以获得:

class Project(models.Model):
    ...
    person_in_project = models.ManyToManyField(User, related_name='project_person', blank=True, through=ProjectPerson)

文档解释了其余的细节,但在这种情况下你不应该处理 limit_choices_to 。

于 2015-09-03T15:01:29.773 回答
0

感谢您的帮助,它非常有用。最有帮助的评论是 ryanmrubin 和使用 through 与 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)
于 2015-09-03T15:21:48.010 回答