admin管理员组文章数量:1356898
I have models for Department and SubDepartment, where each SubDepartment should be linked to a specific Department. However, when selecting a SubDepartment, all available sub-departments are listed instead of filtering them based on the selected Department.
Here are my models, forms, and views:
[enter image description here][1][here before selecting sub department i have to select main department and sub comes under main department.][2]
[1]: .png
[2]: .png
these are the models. models.py
class Department(models.Model):
department_name = models.CharField(max_length=255) # ✅ Correct field
company_name = models.ForeignKey(CompanyName, on_delete=models.CASCADE)
status = models.CharField(max_length=20, choices=[('active', 'Active'), ('inactive', 'Inactive')])
def __str__(self):
return self.department_name
class SubDepartment(models.Model):
sub_department_name=models.CharField(max_length=100,null=False,blank=False)
department_name=models.ForeignKey(Department,on_delete=models.CASCADE)
company_name=models.ForeignKey(CompanyName,on_delete=models.CASCADE)
status=models.CharField(max_length=20, choices=[('active','Active'),('inactive','Inactive')])
def __str__(self):
should i make any change in forms or views?
forms.py
class DepartmentForm(forms.ModelForm):
class Meta:
model = Department
fields = ['department_name', 'company_name', 'status']
class SubDepartmentForm(forms.ModelForm):
class Meta:
model = SubDepartment
fields = ['sub_department_name', 'department_name', 'company_name', 'status']
i already give one to many into the fields but its not working as dependant. here when i select the department the corresponding sub department only should come but without selecting the dept the sub department is listing.
**views.py**
def dept_view(request):
if request.method == "POST":
form = DepartmentForm(request.POST)
if form.is_valid():
form.save()
return redirect('department-view')
else:
form = DepartmentForm()
depts = Department.objects.all()
return render(request, "dept.html", {"form": form, "depts": depts})
# @login_required(login_url="signin")
def delete_department(request, department_id):
if request.method == "POST":
department = get_object_or_404(Department, id=department_id)
department.delete()
return JsonResponse({"success": True})
return JsonResponse({"success": False})
def sub_dept_view(request):
if request.method == "POST":
form = SubDepartmentForm(request.POST)
if form.is_valid():
form.save()
return redirect("sub-department") # Redirect after successful submission
else:
form = SubDepartmentForm()
sub_depts = SubDepartment.objects.all()
return render(request, "sub_department.html", {"form": form, "sub_depts": sub_depts})
def delete_sub_department(request, sub_department_id):
if request.method == "POST":
sub_department = get_object_or_404(SubDepartment, id=sub_department_id)
sub_department.delete()
return JsonResponse({"success": True})
return JsonResponse({"success": False})
版权声明:本文标题:python - Ensuring Dependent Selection of Sub-Departments Based on Main Department in Django - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744056759a2583396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论