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
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?
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.
@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.
Now, import this form in your views.py file
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
After created this filter file, import into your view and handle the checkbox,
Note: I havent tested the code, this is just a skeleton of the logic.