Reputation: 1
Fairly new to django.
contructign view for index page . using 'complex lookups using Q
objects' as in documentation.
Coming across the following error
Request Method: GET Request URL: http://127.0.0.1:8000/music/?q=wretched Django Version: 1.11.12 Exception Type: FieldError Exception Value:
Cannot resolve keyword 'album_title_icontains' into field. Choices are: album_favorite, album_logo, album_title, artist, genre, id, song, user, user_id
Exception Location: /usr/lib64/python2.7/site-packages/django/db/models/sql/query.py in names_to_path, line 1352 Python Executable: /usr/bin/python Python Version: 2.7.15
the function view causing problem is index()
(given below)
def index(request):
if not request.user.is_authenticated():
return render(request, 'music/login_form.html')
else:
albums= Album.objects.filter(user=request.user)
songs=Song.objects.filter(album=albums)
num= Album.objects.filter(user=request.user).count()
req= request.GET.get("q")
if req:
albums= albums.filter(
Q(album_title_icontains=req) |
Q(artist_icontains=req)
).distinct()
songs=songs.filter(
Q(song_title_icontains=req)
).distinct()
context={
'object_list': albums,
'num': num,
'songs': songs
}
return render(request, 'music/index.html', context)
the models.py file file is as given below
from __future__ import unicode_literals
from django.contrib.auth.models import Permission, User
from django.db import models
from django.core.urlresolvers import reverse
class Album(models.Model):
user= models.ForeignKey(User, default=1)
artist= models.CharField(max_length=100)
album_title= models.CharField(max_length=200)
genre= models.CharField(max_length=50)
album_logo= models.FileField()
album_favorite= models.BooleanField(default=False)
def get_absolute_url(self):
return reverse('music:detail',kwargs={'pk':self.pk})
def __str__(self):
return self.album_title+' by '+ self.artist
class Song(models.Model):
album= models.ForeignKey(Album, on_delete=models.CASCADE)
song_title= models.CharField(max_length=200)
audio_file= models.FileField(default='')
is_favorite= models.BooleanField(default=False)
def __str__(self):
return self.song_title
def get_absolute_url(self):
alb= self.album
return reverse('music:detail',kwargs={'pk':alb.pk})
Help would be much appreciated. Thanks in advance.
Upvotes: 0
Views: 416
Reputation: 2357
Your view is correct, just a minor typo. you just needed Double-Underscore :-
__
for your lookups. From docs
Correct - Views.py
def index(request):
if not request.user.is_authenticated():
return render(request, 'music/login_form.html')
else:
albums= Album.objects.filter(user=request.user)
songs=Song.objects.filter(album=albums)
num= Album.objects.filter(user=request.user).count()
req= request.GET.get("q")
if req:
albums= albums.filter(
Q(album_title__icontains=req) |
Q(artist__icontains=req)
).distinct()
songs=songs.filter(
Q(song_title__icontains=req)
).distinct()
context={
'object_list': albums,
'num': num,
'songs': songs
}
return render(request, 'music/index.html', context)
Upvotes: 0
Reputation: 27523
you need dundee('__') double underscore to use the filters.
albums= albums.filter(
Q(album_title__icontains=req) |
Q(artist__icontains=req)
).distinct()
songs=songs.filter(
Q(song_title__icontains=req)
).distinct()
Upvotes: 1