How to setup multi choice field in Django along with Django filter ?

Submitted 3 years, 6 months ago
Ticket #234
Views 2889
Language/Framework Django
Priority Urgent
Status Closed

Hi ,

I want to build the multi choice form in Django template . I mean user should able to select multiple choices in the form and after the submit button clicked it should use the Django Filter to return the data.

Versions

  • Django 2
  • Python 3.6

PFB my code,

# models.py

class Animal(models.Model):
    MY_CHOICES = (
        ('l', 'lion'),
        ('t', 'Tiger'),
        ('e', 'Elephant'),
        ('d', 'Deer'),
    )
    ...
    ...
    my_field = models.CharField(max_length=1, choices=MY_CHOICES)

Now I want to have the multi choice field in Front end, where User should able to select the animal choice based on their requirement and apply the filter should redirect to Django filter and return the respective data.

Could you please help me on this?

Submitted on Oct 17, 20
add a comment

2 Answers

Verified

@Alani25 What I understood is you need to use Checkbox field in your form.

May be this one would help you,

In order to handle the check box , we can use MultipleChoiceField from out of the box from Django form.

from django import forms


MY_CHOICES = (
	('l', 'lion'),
	('t', 'Tiger'),
	('e', 'Elephant'),
	('d', 'Deer'),
)

class AnimalForm(forms.Form):
    
    my_field  = forms.MultipleChoiceField(
        required=False,
        widget=forms.CheckboxSelectMultiple,
        choices=MY_CHOICES,
    )

Now, import this form in your views.py file

form .forms import AnimalForm

def index(request):
   form = AnimalForm()
   return render(request, index.html,{'form':form})

You can render the checkbox in your in html file.

using {{ form.as_p }}

In order to handle the django filter to return the fileter value. Its always better to create filter.py file to handle the filter logic

import django_filters
from . models import Animal

class AnimalFilter(FilterSet):
    status = ChoiceFilter(choices=MY_CHOICES)
    class Meta:
        model = Animal
        fields = ['my_field']

After created this filter file, import into your view and handle the checkbox,

form .forms import AnimalForm
from .filter import AnimalFilter
from .models import Animal

def index(request):
   form = AnimalForm()
   object_list = Animal.objects.all()
   if request.method == 'GET':
       AnimalFilter(
            request.GET,queryset=object_list)
   return render(request, index.html,{'form':form})

Note: I havent tested the code, this is just  a skeleton of the logic.

Submitted 3 years, 6 months ago

May be this will help you !!!

You need to create a many-to-many relationship to a new model, let's call it Acitivity, that represents the available activity that can be dynamically created.

class Activity(models.Model):
my_field = models.CharField(max_length=255)
choices = models.CharField(max_length=3)

Then in your form you can just add that many-to-many field Acitivity. It will by default be a multiple choice field in the django admin.

The relation can have zero to n entries for each pair of Animal and Activity. In other words, every animal can have zero to n activities and vice versa.

To allow for a default value you will have to write a custom migration that creates that default entity of a Activity to make sure it is in the database at runtime.

Submitted 3 years, 6 months ago


Latest Blogs