[Python] How can I set up filtering in the form model of one field based on another when...

Discussão em 'Python' iniciado por Stack, Junho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have 2 model tables. In the first table I have a form in the set in which there is one interesting field. This field can have the same values.

    If we consider the second table model - then in the second table model I placed 2 fields from the first table.

    And in these two fields - there is a field in which there can be duplicate values. I would like, if possible, to enter data into the second table - taking into account: selection of data from the city field and most importantly -

    • so that after selecting in the city field - some filtering of the second field occurs. For example, filtering of two fields occurs according to the selected first field and is substituted into the second field.

    How can this be done. I select the city field - and so that the second field is filtered.

    How can I set up filtering in the form model of one field based on another when selecting?

    Help me please, I will be glad to any hint on how to do this?

    from django.db import models

    # Create your models here.


    TYPE_OBJECT = [
    ("1", "Котельная"),
    ("2", "Сети"),
    ("3", "БМК"),
    ("4", "ЦТП"),
    ("5", "ТГУ"),
    ]

    TYPE_WORK = [
    ("1", "Реконструкция"),
    ("2", "Капитальный ремонт"),
    ("3", "Строительство"),
    ]

    TYPE_CITY = [
    ("1", "Бронницы"),
    ("2", "Луховицы"),
    ("3", "Павловский Посад"),
    ("4", "Раменское"),
    ("5", "Шатура"),
    ]

    class ArkiOneObject (models.Model):
    city = models.CharField(
    choices=TYPE_CITY,
    verbose_name="Выберите ОМСУ")
    typeobject = models.CharField(
    choices=TYPE_OBJECT,
    verbose_name="Выберите тип объекта")
    typework = models.CharField(
    choices=TYPE_WORK,
    verbose_name="Выберите тип работ")
    nameobject = models.TextField(verbose_name="Наименование объекта")
    def __str__(self):
    return self.nameobject, self.city



    class ArkiTwoObject (models.Model):
    city = models.OneToOneField(ArkiOneObject, verbose_name="Выберите ОМСУ", on_delete=models.CASCADE)
    nameobject = models.OneToOneField(ArkiOneObject, verbose_name="Наименование объекта", on_delete=models.CASCADE)
    power_lenth = models.FloatField(verbose_name="Мощность или длина")
    potr_msd = models.IntegerField(verbose_name="Кол-во Потребители МСД")
    potr_cityzen = models.IntegerField(verbose_name="Кол-во Жители")
    potr_social = models.IntegerField(verbose_name="Кол-во Социальные")
    value_budget = models.FloatField(verbose_name="Объём финансирования")
    podryadchik = models.CharField(verbose_name="Подрядчик")
    date_contract = models.DateField(verbose_name="Дата заключения контракта")
    zena_contr = models.FloatField(verbose_name="Цена контракта")


    from django import forms
    from .models import *


    class FormOne (forms.ModelForm):
    class Meta:
    model = ArkiOneObject
    fields = "__all__"



    class FormTwo (forms.ModelForm):
    class Meta:
    model = ArkiTwoObject
    fields = "__all__"


    from django.shortcuts import render, redirect
    from django.template import context
    from django.http import HttpResponse
    import pandas as pd
    from .models import *
    from .forms import *

    def index(request):
    context={}

    return render(request, 'index.html', context)



    def formone(request):
    context = {}
    formone = FormOne(request.POST or None)
    if formone.is_valid():
    formone.save()

    return redirect("formone")
    context['formone'] = formone
    return render(request, "formone.html", context)


    def formtwo(request):
    context = {}
    formtwo = FormTwo(request.POST or None)
    if formtwo.is_valid():
    formtwo.save()

    return redirect("formtwo")
    context['formtwo'] = formtwo
    return render(request, "formtwo.html", context)


    def table(request):
    context = {}
    table = ArkiTwoObject.objects.all()
    table = pd.DataFrame.from_records(table.values())

    context['table'] = table

    table_2 = ArkiOneObject.objects.all()
    table_2 = pd.DataFrame.from_records(table_2.values())
    print(table_2)

    context['table_2'] = table_2
    return render(request, "table.html", context)


    # Create your views here.

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <title>title</title>
    </head>
    <body>
    <div>
    <form method="POST" class="post-form">
    {% csrf_token %} {{formone.as_p}}
    <button type="submit" class="button">Отправить</button>
    </form>
    </div>

    </body>
    </html>

    Continue reading...

Compartilhe esta Página