I wanted to make a search bar which search the database for queries in django and have come up with this so far:
view:
class IndexView(LoginRequiredMixin, generic.ListView):
template_name = 'patients/index.html'
context_object_name = 'all_patients'
def get_queryset(self):
queryset = patient.objects.all()
query = self.request.GET.get('q')
if query:
queryset = queryset.filter(
Q(First_Name__icontains=query) |
Q(Surname__icontains=query) |
Q(DOB__icontains=query)
).distinct()
return queryset
else:
return patient.objects.all()
and my button on my template:
<form class="navbar-form navbar-left" role="search" method="get" action="{% url 'patients:index' %}">
<div class="form-group">
<input type="text" class="form-control" name="q" value="{{ request.GET.q }}">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
and the model:
class patient(models.Model):
TITLE_CHOICES = (
('Mr', 'Mr'),
('Mrs', 'Mrs'),
('Ms', 'Ms'),
('Miss', 'Miss'),
)
Title = models.CharField(max_length=100, blank=True, choices=TITLE_CHOICES)
First_Name = models.CharField(max_length=250, default='')
Surname = models.CharField(max_length=250, default='')
DOB = models.DateField()
...
def get_absolute_url(self):
return reverse('patients:patientInfo', kwargs={"pk":self.pk})
def __str__(self):
return self.First_Name + " " + self.Surname
I am getting the error:
TypeError: 'QuerySet' object is not callable
I have no clue what I am doing wrong and am very new to Django.
I encountered this problem when I added a property, then it's a method and can't be callable
@property
def get_queryset(self):
so I fixed it by removing the property
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.