Related Field has invalid lookup: icontains
--------------------------------------------
Exception Type: TypeError
Exception Value:
Related Field has invalid lookup: icontains
=========================== Example 1
solution:ForeignKey
-------------------
> I have code for searching:
>
> news = News.objects.filter(
> Q(title__icontains=phrase)
> | Q(tags__icontains=phrase)
> | Q(short__icontains=phrase)
> | Q(content__icontains=phrase)
> | Q(author__icontains=phrase)
> | Q(source__icontains=phrase)
> )
> News model looks like that:
>
> class News(models.Model):
> title = models.CharField(maxlength="100")
> tags = models.CharField(maxlength="75")
> short = models.TextField()
> content = models.TextField()
> author = models.ForeignKey(User, editable=False)
> source = models.CharField(maxlength="100")
> date = models.DateTimeField(auto_now_add=True)
> status = models.IntegerField(editable=False, default="0")
> def __str__(self):
> return '%s' % (self.title)
> class Admin():
> pass
>
You need to specify which fields in the User/Author table to match on
(the default is the primary key , which doesn't make sense here).
Something like
|Q(author__username__icontains=phrase)
------------------
Okay. It works!
The missing part in my code was:
Q(author__username__icontains=phrase). I couldn't find it. Thank you
for your help.
=========================== Example 2
solution:ManyToMany
-------------------
-------------------- model
class Book(models.Model):
library = models.ForeignKey(Library)
accession_no = models.CharField(max_length=200, blank=True, null=True)
type = models.CharField(max_length=500, blank=True, null=True)
author = models.ManyToManyField('Contributor', related_name='book_authors')
editor = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_editors')
general_editor = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_general_editors')
reviser = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_revisers')
compiler = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_compilers')
translator = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_translators')
illustrator = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_illustrators')
forward = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_forwards')
interpreter = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_interpreters')
cover_designer = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_cover_designers')
commentry = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_commentries')
preface = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_prefaces')
introduction = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_introductions')
prologue = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_prologues')
reteller = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_retellers')
cover_photographer = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_cover_photographers')
title = models.CharField(max_length=500)
publisher = models.ManyToManyField('Publisher', related_name='book_publishers')
class_no = models.CharField(max_length=200, blank=True, null=True)
----
---- etc--
-------------------- view
search_data = request.GET.copy()
keyword = search_data['search-query']
l = ["class_no", "accession_no", "title", "compiler__name", "reviser__name", "editor__name", "author",
"translator__name", "illustrator__name", "forward__name", "interpreter__name", "cover_designer__name", "type",
"title_of_orig", "reprint", "edition", "first_publ_year", "place", "publisher__name",
"year_of_publ", "no_of_vols_in_set", "pages", "illus", "series", "series_editor",
"lang_of_orig", "notes", "isbn", "publ_code", "price", "distributor", "appendix",
"introduction__name", "errata", "preface__name", "prologue__name", "publ_sl_no", "month_of_publ",
"commentry__name"]
q = Q()
for x in l:
q = q | Q(**{x+"__icontains":keyword})
print "qry", q
queryset=Book.objects.filter(q & Q(library=library_id)
--------------------
**************************************************************
No comments:
Post a Comment