[Python] here i have some models and its linked with other models. each model is dependant...

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    [enter image description here][1][here before selecting sub department i have to select main department and sub comes under main department.][2]


    [1]: https://i.sstatic.net/3GSQjtkl.png
    [2]: https://i.sstatic.net/Tp9kg2uJ.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})

    Continue reading...

Compartilhe esta Página