How to filter data from model based on web page user selection in a form in Django?

I have Data model with records and these records are presented in web page. I have created function which prepares table with records from Data model and returns required structure of these data in pu.html web page.

And I would like to filter these data based on user selection in a form on the same webpage.

Therefore I created second model Manager_selection where user selection is stored and I get from this model last value based on which data are filtered in my function which prepares table with records.

And what I need is to somehow include function for form in Views to be able to save user selection and based on which data will be filtered as my View is based on model Data, but form is based on model Manager_selection.

  1. Data model - includes detail data for each project (date, document_number, amount, project, account, company,...)

  2. Projects model - includes data about project (code, mo - project manager, vps - site manager, ...). This model is connected with Data model 1:N connection, so one project can have multiple Data records

  3. Employee model - includes first, second name and position - it is connected with Project model 1:N - you can select mo (project manager) and vps (site manager) from Employee model for each project and each project has just one mo and vps.

  4. Manager_selection - this model includes mo (project manager) and vps (site manager) selected by user in a form - I get the last value from this model and filter based on these two names data which are presented in the web page.

class Manager_Selection(models.Model):

    def mo_selection_list_creation():
        mo_data = Employees.objects.values()
        mo_list = []
        for i in mo_data:
            if i["position"] == "MO":
                mo_list.append([str(i["name"]+" "+i["surname"]), str(i["name"]+" "+i["surname"])])
        return mo_list

    def vps_selection_list_creation():
        vps_data = Employees.objects.values()
        vps_list = [], ]
        for i in vps_data:
            if i["position"] == "VPS":
                vps_list.append([str(i["name"]+" "+i["surname"]), str(i["name"]+" "+i["surname"])])
        return vps_list

    mo_list = mo_selection_list_creation()
    vps_list = vps_selection_list_creation()

    mo = models.CharField("MO", choices=mo_list, max_length=50, blank=True, null=True)
    vps = models.CharField("VPS", choices=vps_list, max_length=50, blank=True, null=True)

    def __str__(self):
        return f"{}, {self.vps}"

Views - it takes data from Data model and prepares table based on table_creation() function;

class PuView(LoginRequiredMixin, FormView):

    template_name = "pu.html"
    model = Data
    success_url = reverse_lazy("pu")
    form_class = ManagerSelectionForm

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        entry_data = Data.objects.values() 
            mo = Manager_selection.objects.values().last()["mo"]
            vps = Manager_selection.objects.values().last()["vps"]
            mo = None
            vps = None
            data = table_creation(entry_data, mo, vps)
            data = 0
        context["data"] = data

        return context

form is based on Manager_Selection model:

class ManagerSelectionForm(ModelForm):
    class Meta:
        model = models.Manager_Selection
        fields = ("mo", "vps")

and form is included to html page:

<form method="post">
                {% csrf_token %}
                <div class="input-group p-0 m-0">
                    <span class="pt-2 pe-2">Výběr MO, VPS: </span>
                    {{ }}
                    {{ form.vps }}
                    <button class="btn btn-outline-dark" type="submit"><i class="fa-solid fa-file-import"></i> Odeslat</button>

So to sum up:

  1. is there some way how to do this in a more simple way? - to be able to filter records from Data model based on web page user selection?
  2. if my way is OK, how to include function for saving of data from the form to Manager_selection model in my View which is based on different model Data and I am using there get_context_data() function?
Back to Top