in this code class EmployeeProfile(MPTTModel): choice = [ ('نعم', 'نعم'), ('لا', 'لا'),…
- User
in this code class Employee_Profile(MPTTModel): choice = [ ('نعم', 'نعم'), ('لا', 'لا'), ] user = models.ForeignKey(User, on_delete=models.CASCADE) action_creator =models.CharField(max_length=250, null=True, blank=True) rawaf_id = models.IntegerField(null=True, blank=True, unique=True) iqama_no = models.IntegerField(null=True, blank=True, unique=True) full_name = models.CharField(max_length=250, null=True, blank=True) parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children', limit_choices_to=his children) job_name = models.CharField(max_length=250, null=True, blank=True) oc_level = models.IntegerField(null=True, blank=True) levels_nt_show = models.IntegerField(null=True, blank=True, default=0) his_chart = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='hischart') on_work = models.CharField(max_length=250, null=True, blank=True, choices=choice, default='نعم')
def __str__(self): return ", ".join([str(self.job_name), str(self.full_name)]) class MPTTMeta: verbose_name="rawaf_id" ordering = ['parent','oc_level']need to make parent field limit choices from his child only
- Assistant
To limit the choices for the parent field only to the children of the current instance, you can override the
formfieldmethod of theparentfield in theEmployee_Profileclass. Here's an updated version of your code:class Employee_Profile(MPTTModel): choice = [ ('نعم', 'نعم'), ('لا', 'لا'), ] user = models.ForeignKey(User, on_delete=models.CASCADE) action_creator = models.CharField(max_length=250, null=True, blank=True) rawaf_id = models.IntegerField(null=True, blank=True, unique=True) iqama_no = models.IntegerField(null=True, blank=True, unique=True) full_name = models.CharField(max_length=250, null=True, blank=True) parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children', limit_choices_to={'children__isnull': False}) # Updated limit_choices_to parameter job_name = models.CharField(max_length=250, null=True, blank=True) oc_level = models.IntegerField(null=True, blank=True) levels_nt_show = models.IntegerField(null=True, blank=True, default=0) his_chart = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='hischart') on_work = models.CharField(max_length=250, null=True, blank=True, choices=choice, default='نعم') def __str__(self): return ", ".join([str(self.job_name), str(self.full_name)]) class MPTTMeta: verbose_name = "rawaf_id" ordering = ['parent', 'oc_level'] def formfield(self, **kwargs): kwargs['queryset'] = Employee_Profile.objects.filter(parent=self) return super().formfield(**kwargs)In the above code, I've added the
limit_choices_toparameter to theparentfield, which filters the available choices to only show the children of the current instance.I've also overridden the
formfieldmethod of theparentfield and set thequerysetparameter to filter the choices based on the current instance's children.By doing this, the parent field will only display the children of the current instance as options.