0

I am new to Django and struggling to figure out how to query a database and then return a filtered list based off of drop-down selections. I have tried doing it with forms but I'm not sure this is necessary as I do not need to store the drop-down data- I just want to use it to query a database and filter it accordingly. So if user selects "dog" in the "type" drop-down I would then query the Dog db and then filter it based off of the second drop-down selection of "age_group". So, if user selected "dog" and then "baby" it would return a list of puppies. dog.model

    class Dog(models.Model):
        name =models.CharField(max_length=200,validators=[MinLengthValidator(2, "Nickname must 
        be greater than 1 character")])
        breed_group = models.CharField(max_length=200, choices=BreedGroup.choices, null=False, 
        blank=False, default='Not Enough Information')
        breeds = models.ForeignKey(DogBreed, on_delete=models.PROTECT)
        age   = models.PositiveIntegerField()
        ageGroup = models.IntegerField(choices=AgeGroup.choices, null=False, blank=False, 
        default=0)
        sex = models.IntegerField(choices=SEX, blank=False, null=False, default=0)
        tagLine = models.CharField(max_length=150)
        goodWithCats = models.BooleanField(blank=False, null=False, default='Not Enough 
        Information')
        goodWithDogs = models.BooleanField(null=False, blank=False, default='Not Enough 
        Information')
        goodWKids = models.BooleanField(null=False, blank=False, default='Not Enough 
        Information')

        def __str__(self):
            return self.name

search.html

         <div class="row" >
          <div class="col-md-12 justify-content-sm-center">
            <div class="input-group" id="searchBox">
              <div class="col-xs-1 mx-4"></div>
                <form class="form-inline" action="." method="GET">
                    <div class = "justify-content-sm-center">
                        <label class="sr-only type " for="type" >TYPE</label>
                        <select class="custom-select my-1 mr-sm-2" id="type2">
                            <option selected>ALL TYPES</option> # queries all 
                            <option value="1">CAT</option> # queries class CAT
                            <option value="2">DOG</option> # queries class Dog
                            <option value="3">OTHER</option> # queries class Other
                        </select>
                    </div>
                    <div class = "justify-content-sm-center"></div>
                        <label class="sr-only agelabel " for="age">AGE</label>
                            <select class="custom-select my-1 mr-sm-2" id="agelabel2">
                                <option selected>ANY AGE</option> # returns all listings
                                <option value="1">BABY</option> # returns babies of selected 
                                 type
                                <option value="2">ADULT</option> #'' adults ''
                                <option value="3">SENIOR</option>#'' seniors ''
                            </select>

                    <button type="submit" class="btn btn-primary mb-2">SEARCH</button>

                </form>
            </div>
          </div>
        </div>
Zuckerbrenner
  • 323
  • 2
  • 15

1 Answers1

1

Assuming you are able to capture model_name and age_group values from the form, you can use the below code in the view to get the queryset

from django.apps import apps

model = apps.get_model(app, model_name)
qs = model.objects.filter(age_group=age_group)
Arjun Ariyil
  • 386
  • 3
  • 14
  • Thank you! This helped but I now seem to not be able to capture the choice selections made from template to view. I am using ModelForm with fields = ['type', 'ageGroup']. While it displays the text choices for each field correctly, I cannot figure out how to grab the selected option when the user clicks "search". I don't see that the options within 'type' and the options within 'ageGroup' have individual ids. – Zuckerbrenner Dec 27 '20 at 04:38
  • I changed much of the code at this point, completely changed the model to reduce redundancy. This is my first Django project (if that is not embarrassingly obvious). Thank you for taking the time to help me. This is my problem here: https://stackoverflow.com/questions/65483354/how-best-to-capture-variables-from-within-a-for-loop-in-django-template – Zuckerbrenner Dec 28 '20 at 20:03