Reputation: 1338
I have a project model. This project contains persons (those who are working on the project). I am trying to also make a model for each project person, including any notes they have on the project and % complete on project.
My issue is that I want to filter the individual_person_in_project
to only the persons within the corresponding project. I am trying to use
limit_choices_to = {'person_in_project':User}
I want to limit my choices to users who are persons in my Project
model.
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)
Upvotes: 0
Views: 948
Reputation: 1338
Thank you for your help, it was very useful . The most helpful comment was ryanmrubin and the use of through with ManyToManyField to facilitate their relationship I ended up creating a separate class and associating that with a project.
If I need to tie more information into this new class I will certainly use through with the 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)
Upvotes: 0
Reputation: 728
I left a comment above, but I think this is a better answer, anyhow:
You can use the through option to track extra information on the manytomanyfield, so you get:
class Project(models.Model):
...
person_in_project = models.ManyToManyField(User, related_name='project_person', blank=True, through=ProjectPerson)
The docs explain the rest of the details, but you shouldn't have to handle the limit_choices_to in that case.
Upvotes: 0