Reputation: 189
I have class in models.py:
class Companies(models.Model):
id = models.AutoField(unique=True, primary_key=True, null=False, blank=False)
is_active = models.BooleanField(default=1, editable=False)
In HTML template have this radiogroup:
<fieldset>
<legend>Status</legend>
<input type="radio" name="is_active" value="">All</label>
<input type="radio" name="is_active" value="True" checked="1">Active</label>
<input type="radio" name="is_active" value="False">Not Active</label>
</fieldset>
using jquery i want to serialize() radiogroup and send to tastypie API to get filtered data from model:
URL from query then will look like this:
http://localhost:8000/api/view/company/list/?is_active=
result will show only rows with False values in is_active field
if i use ?is_active=1 results will be only True
How can i get both True and False rows from table?
i can change "name" attr in input, but name must be the same in all inputs to stay grouped.
Upvotes: 1
Views: 922
Reputation: 1860
If you pass ?is_active=
on the Django side the request will have 'is_active' in POST dictionary:
>>> 'is_active' in request.POST`
True
The problem is the content is string which is empty here:
>>> request.POST['is_active']
'' # Or None I am not quite sure.
And according to Python semantic:
>>> bool(None)
False
>>> bool(False)
False
>>> bool(0)
False
>>> bool('')
False
All negative values are: []
, ()
, {}
, set()
, 0
, ''
, None
, False
You have to override build_filters
to remove this key if its empty or don't call API with that value being empty.
def build_filters(self, filters=None):
if filters is None:
filters = {}
if "is_active" in filters and filters["is_active"] == '': # or == None you have to check
del filters['is_active']
return super(MyResource, self).build_filters(filters)
Upvotes: 1