Skip to content

Commit fea817c

Browse files
committed
🔨 ISBN added for an example of exact filter
1 parent 512c401 commit fea817c

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

db.sqlite3

8 KB
Binary file not shown.

library/libraryapp/admin.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ class AuthorAdmin(admin.ModelAdmin):
2020
@admin.register(Book)
2121
class BookAdmin(admin.ModelAdmin):
2222
save_on_top = True
23-
list_display = ["title", "publishing_date"]
23+
list_display = ["title", "get_authors", "isbn", "publishing_date"]
2424
list_filter = ["authors"]
2525
search_fields = ["title",]
2626
fieldsets = [
27-
(_("Content"), {'fields': ["title", "authors", "publishing_date"]}),
27+
(_("Content"), {'fields': ["title", "authors", "isbn", "publishing_date"]}),
2828
]
29-
filter_horizontal = ["authors"]
29+
filter_horizontal = ["authors"]
30+
31+
def get_authors(self, obj):
32+
return ", ".join([author.author_name for author in obj.authors.all()])
33+
get_authors.short_description = _("Authors")

library/libraryapp/documents.py

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class BookDocument(DocType):
4444
'pk': fields.IntegerField(),
4545
}, include_in_root=True)
4646

47+
isbn = fields.StringField(
48+
index='not_analyzed',
49+
)
50+
4751
class Meta:
4852
model = Book # The model associated with this DocType
4953

@@ -58,3 +62,6 @@ def get_instances_from_related(self, related_instance):
5862
"""If related_models is set, define how to retrieve the Book instance(s) from the related model."""
5963
if isinstance(related_instance, Author):
6064
return related_instance.book_set.all()
65+
66+
def prepare_exact_title(self, obj):
67+
return obj.title
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('libraryapp', '0002_book_publishing_date'),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name='book',
16+
name='isbn',
17+
field=models.CharField(max_length=20, verbose_name='ISBN', blank=True),
18+
),
19+
]

library/libraryapp/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Book(models.Model):
2626
title = models.CharField(_("Title"), max_length=200)
2727
authors = models.ManyToManyField(Author, verbose_name=_("Authors"))
2828
publishing_date = models.DateField(_("Publishing date"), blank=True, null=True)
29+
isbn = models.CharField(_("ISBN"), blank=True, max_length=20)
2930

3031
class Meta:
3132
verbose_name = _("Book")

library/libraryapp/views.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
import operator
55

6+
from django.http import Http404
67
from django.utils.translation import ugettext_lazy as _
78
from django.shortcuts import render
89
from django.core.paginator import Paginator, Page, EmptyPage, PageNotAnInteger
910
from django import forms
1011

1112
from crispy_forms import helper
1213

14+
from elasticsearch import TransportError
1315
from elasticsearch_dsl.query import Q
1416

1517
from .models import Author, Book
@@ -26,6 +28,10 @@ class SearchForm(forms.Form):
2628
label=_("Title"),
2729
required=False,
2830
)
31+
isbn = forms.CharField(
32+
label=_("ISBN"),
33+
required=False,
34+
)
2935
authors = forms.ModelMultipleChoiceField(
3036
label=_("Authors"),
3137
queryset=Author.objects.all(),
@@ -64,6 +70,10 @@ def book_list(request):
6470
if title:
6571
search = search.query("fuzzy", title=title)
6672

73+
isbn = form.cleaned_data['isbn']
74+
if isbn:
75+
search = search.query("match", isbn=isbn)
76+
6777
authors = form.cleaned_data['authors']
6878
if authors:
6979
author_queries = []
@@ -88,13 +98,16 @@ def book_list(request):
8898
paginator = Paginator(search_results, paginate_by)
8999
page_number = request.GET.get("page")
90100
try:
91-
page = paginator.page(page_number)
92-
except PageNotAnInteger:
93-
# If page parameter is not an integer, show first page.
94-
page = paginator.page(1)
95-
except EmptyPage:
96-
# If page parameter is out of range, show last existing page.
97-
page = paginator.page(paginator.num_pages)
101+
try:
102+
page = paginator.page(page_number)
103+
except PageNotAnInteger:
104+
# If page parameter is not an integer, show first page.
105+
page = paginator.page(1)
106+
except EmptyPage:
107+
# If page parameter is out of range, show last existing page.
108+
page = paginator.page(paginator.num_pages)
109+
except TransportError:
110+
raise Http404('Index does not exist. Run `python manage.py search_index --rebuild` to create it.')
98111

99112
context = {
100113
'object_list': page,

library/templates/base.html

+2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
{% block page %}
2323
<div class="wrapper">
2424
<div id="header" class="clearfix">
25+
<hr />
2526
<h1 class="text-center">{% trans "Library of Django Books" %}</h1>
27+
<hr />
2628
{% block header_navigation %}
2729
{% endblock %}
2830
{% block language_chooser %}

library/templates/library/book_list.html

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ <h2>Filtered results ({{ object_list.paginator.count }})</h2>
3131
{% for author in book.authors %}
3232
{{ author.author_name }}{% if not forloop.last %},{% endif %}
3333
{% endfor %}
34+
{% if book.isbn %}<p>ISBN: {{ book.isbn }}</p>{% endif %}
3435
</div>
3536
</div>
3637
{% endfor %}

0 commit comments

Comments
 (0)