Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Filter within a Filter in Django

I’m trying to filter category with age range. I want to see people with certain genders and then their age range.

My models:

from django.db import models

# Create your models here.

class catagory(models.Model):
    name = models.CharField(max_length=150)

    def __str__(self):
        return self.name


class Age(models.Model):
    range = models.CharField(max_length=150)

    def __str__(self):
        return self.range


class person(models.Model):
    name = models.CharField(max_length=100)
    gender = models.ForeignKey(catagory, on_delete=models.CASCADE)
    age_range = models.ForeignKey(Age, on_delete=models.CASCADE)

    @staticmethod
    def get_person_by_cataegoryID(categoryID):
        if categoryID:
            return person.objects.filter(gender=categoryID)
        else:
            return person.objects.all()

    @staticmethod
    def get_person_by_age_range(AGE_RANGE):
        if AGE_RANGE:
            return person.objects.filter(age_range=AGE_RANGE)
        else:
            return person.objects.all()

    def __str__(self):
        return self.name

My views

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    from django.shortcuts import render
from .models import catagory,person,Age

# Create your views here.

def index(request):
    c = catagory.objects.all()
    categoryID = request.GET.get('category')
    AGE_RANGE = request.GET.get('age_range')



    p= person.objects.all()
    a = Age.objects.all()
    if categoryID:
        persons = person.get_person_by_cataegoryID(categoryID)
    elif AGE_RANGE:
        persons = person.get_person_by_age_range(AGE_RANGE)
    else:
        persons = person.objects.all()

    context = {
        "person": p,
        "catagory": c,
        "age": a
    }

    context["person"] = persons

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

My Template

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>hello</h2>
<div style="display:flex;height:100vh;width:100vw;align-items:center;justify-content:space-around;">

    <div style="display:flex;flex-direction:column;">
        {% for a in age %}
        <a href="/?age_range={{a.id}}">{{a.range}}</a>
        {% endfor %}
    </div>
  <div style="display:flex;flex-direction:column;">
      <a href="/">All</a>
    {% for c in catagory %}
      <a href="/?category={{c.id}}">{{c.name}}</a>
    {% endfor %}
  </div>
  <br>
  <div style="display:flex;flex-direction:column;">
    {% for p in person %}
      <p>{{p.name}}</p>
     {% endfor %}

  </div>
</div>

</body>
</html>

Please tell me what I’m doing wrong here. The filters work by themselves correctly but my goal is to see a specific gender and then that gender’s age range. Currently it shows the gender and age range of the whole group.

>Solution :

if you need gender and age at the same time:

if categoryID and AGE_RANGE:
    persons = person.objects.filter(gender=categoryID, age_range=AGE_RANGE)     
elif categoryID:
    persons = person.get_person_by_cataegoryID(categoryID)
elif AGE_RANGE:
    persons = person.get_person_by_age_range(AGE_RANGE)
else:
    persons = person.objects.all()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading