Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在django mptt中,如何将子节点号固定到父节点号?_Python_Python 3.x_Django_Django Mptt - Fatal编程技术网

Python 在django mptt中,如何将子节点号固定到父节点号?

Python 在django mptt中,如何将子节点号固定到父节点号?,python,python-3.x,django,django-mptt,Python,Python 3.x,Django,Django Mptt,我想为每位家长安排两个孩子 Models.py class CoachingRegistration(MPTTModel): uid = models.UUIDField(unique=True, editable=False, default=uuid.uuid4) placement_id = models.CharField(max_length=13) parent = TreeForeignKey('self', on_delete=models.CASCAD

我想为每位家长安排两个孩子

Models.py

class CoachingRegistration(MPTTModel):
    uid = models.UUIDField(unique=True, editable=False, default=uuid.uuid4)
    placement_id = models.CharField(max_length=13)

    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by = ['placement_id']

    def __str__(self):
        return str(self.placement_id) 

请帮助我如何修复此问题。

您可以在createview中使用form\u有效函数修复此问题

class CoachingRegistrationCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView):
    model = CoachingRegistration
    form_class = CoachingRegistrationForm
    template_name = 'customer/form_coaching_register.html'

    def form_valid(self, form):
        if 2 > CoachingRegistration.objects.filter(parent=CoachingRegistration.objects.get(coaching_id=form.instance.placement_id)).count():
            return super().form_valid(form)
        else:
            return super().form_invalid(form)

你的电脑里有个bugcode@user1241241你能告诉我在哪里以及如何修理吗??