Reputation: 816
I'm currently trying to implement a search functionality to my Django app. Everything works perfectly fine, until the actual search query is handled. Say, I search for "hallo". Django would the return the following error:
django.core.exceptions.FieldError
FieldError: Cannot resolve keyword 'title' into field. Choices are: adresse, arbejde, beskrivelse, email, foerste_session, fulde_navn, id, profilbillede, relateret_til_andre_klienter, tidligere_klient, vurder_sidste_session
There seems to be some sort of conflict between my form (I am using a ModelForm) and my search function. How do I go about solving my problem?
Here is my forms.py:
from django import forms
from .models import Client
class ClientForm(forms.ModelForm):
class Meta:
model = Client
fields = (
'fulde_navn',
'adresse',
'email',
'tidligere_klient',
'foerste_session',
'beskrivelse',
'arbejde',
'relateret_til_andre_klienter',
'vurder_sidste_session',
'profilbillede'
)
And here is my models.py
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
import uuid
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
import os
import uuid
YESNO_CHOICES = (
(0, 'Ja'),
(1, 'Nej')
)
SESSION_CHOICES = (
(0, '1'),
(1, '2'),
(2, '3'),
(3, '4'),
(4, '5'),
)
def upload_to_location(instance, filename):
blocks = filename.split('.')
ext = blocks[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
instance.title = blocks[0]
return os.path.join('uploads/', filename)
# Create your models here.
class Client(models.Model):
fulde_navn = models.CharField('Navn', max_length=75)
adresse = models.CharField(max_length=100)
email = models.EmailField(null=True, blank=True)
tidligere_klient = models.IntegerField(choices=YESNO_CHOICES, null=True, blank=True)
foerste_session = models.DateField('Dato for 1. session', null=True, blank=True)
beskrivelse = models.TextField('Noter', null=True, blank=True)
arbejde = models.CharField('Arbejde', max_length=200)
relateret_til_andre_klienter = models.IntegerField(choices=YESNO_CHOICES, null=True, blank=True)
vurder_sidste_session = models.IntegerField(choices=SESSION_CHOICES, null=True, blank=True)
profilbillede = models.ImageField('Profilbillede',
upload_to='profile_pics/%Y-%m-%d/',
null=True,
blank=True)
def __unicode__(self):
return self.fulde_navn
def get_absolute_url(self):
return reverse(viewname="client_detail", args=[self.id])
And finally my views.py where my search function is located:
from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic.base import TemplateView
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from . import models
from .forms import ClientForm
# Create your views here.
class ClientsListView(ListView):
model = models.Client
template_name = 'clients/list.html'
paginate_by = 20
class SearchListView(ClientsListView):
def get_queryset(self):
incoming_query_string = self.request.GET.get('query', '')
return models.Client.objects.filter(title__icontains=incoming_query_string)
class ClientsDetailView(DetailView):
model = models.Client
template_name = 'clients/detail.html'
def client_create(request):
if request.method == 'POST':
form = ClientForm(request.POST)
if form.is_valid():
client = form.save()
client.save()
return redirect('client_detail', pk=client.pk)
else:
form = ClientForm()
return render(request, 'clients/form.html', {'form': form})
def client_edit(request, pk):
client = get_object_or_404(models.Client, pk=pk)
if request.method == "POST":
form = ClientForm(request.POST, instance=client)
if form.is_valid():
client = form.save()
client.save()
return redirect('client_detail', pk=client.pk)
else:
form = ClientForm(instance=client)
return render(request, 'clients/form.html', {'form': form})
class ClientsUpdateView(UpdateView):
model = models.Client
template_name = 'clients/form.html'
How do I solve this problem? I want to make it so my user is able to search for clients in my list.
Upvotes: 2
Views: 82
Reputation: 2233
In this line:
return models.Client.objects.filter(title__icontains=incoming_query_string)
You are trying to check if the field title
contains the incoming_query_string
. However, your ClientForm
doesn't have such field.
You should use one of the fields you listed there or add title
as a field.
Upvotes: 1