1๐
โ
Filter methods are chainable, immutable ๐
def search_user(name=None, number=None):
# Uncomment the following lines to return NO records when no input.
# if not name and not number:
# return UserInfo.objects.none()
qs = UserInfo.objects.all()
if name:
qs = qs.filter(name=name)
if number:
qs = qs.filter(number=number)
return qs
This example would return all records if no input.
(Note that QS are lazy, and all() would not retrieve all records unless accessed later on.)
๐คlaffuste
1๐
Q is a powerful feature in Django. see Q class
from django.db.models import Q
# you cand do a lot of crazy things here
query = Q(name__iexact=name) & Q(number=number)
users = UserInfo.object.filter(query)
๐คRafael
- [Answered ]-Django-Compressor throws UncompressableFileError with django-storages using amazonaws and heroku
- [Answered ]-Is it ever necessary to instantiate new HttpRequest objects in Django?
0๐
I would do something like that
if name and number:
UserInfo.object.filter(name='John',number='1234')
๐คcor
0๐
The solution according to your example isโฆ
MatchedUsers = UserInfo.object.filter(name='John',number='1234')
return MatchedUsers
๐คPawan
- [Answered ]-Updating fields of model using forms and views on Django 1.7
- [Answered ]-How to make multiple model queries in a class based view(template view)
Source:stackexchange.com