gpt4 book ai didi

python - 如何在 django-mptt 中修复没有 child 的 parent ?

转载 作者:行者123 更新时间:2023-12-04 09:36:17 31 4
gpt4 key购买 nike

我想为每个 parent 修复 2 个 child 。
模型.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)
请帮我解决这个问题。

最佳答案

您可以使用 form_valid 函数在 createview 中修复此问题

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)

关于python - 如何在 django-mptt 中修复没有 child 的 parent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62574940/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com