Could you please help me to understand how do limit choices work in the model if it is necessary to limit by field of another model. Currently, I have two models:
Group:
class Group(models.Model):
name = models.CharField(max_length=10, null=True)
leader = models.OneToOneField(
'Student',
related_name='+',
null=True,
blank=True,
on_delete=models.SET_NULL,
limit_choices_to={'group': 2},
)
and Students:
class Student(models.Model):
group = models.ForeignKey(Group, on_delete=models.PROTECT)
name = models.CharField(max_length=140, null=True)
brd_date = models.DateField(null=True)
ticket = models.IntegerField(null=True, unique=True)
So as you see every Student has it's group (configured by ForeignKey). But every group may or may not have its own leader. It's just one specific Student (configured by OneToOneField) who is chosen from the group to which he/she actually belongs. Without limiting by Group, it becomes possible to choose every Student as a leader to every group. In the provided Group model I've set limit_choices_to options to limit choices just to the Group 2 (for testing purposes).
Is it possible to set this parameter dynamically and as a result have a possibility of creating Form class based on Group Model in which selection for Group leader allows choosing only students in this particular group?
Thanks in advance.