kopia lustrzana https://github.com/wagtail/wagtail
Replace search test models
rodzic
522a6868af
commit
fe1953a70c
|
@ -0,0 +1,483 @@
|
|||
[
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "George R.R. Martin",
|
||||
"date_of_birth": "1948-09-20"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "J. R. R. Tolkien",
|
||||
"date_of_birth": "1892-01-03"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 4,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "Isaac Asimov",
|
||||
"date_of_birth": "1920-01-02"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 5,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "David Ascher",
|
||||
"date_of_birth": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 6,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "Mark Lutz",
|
||||
"date_of_birth": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 7,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "Daniel Roy Greenfeld",
|
||||
"date_of_birth": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 8,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "Audrey Roy Greenfeld",
|
||||
"date_of_birth": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 9,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "Douglas Crockford",
|
||||
"date_of_birth": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 10,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "David Flanagan",
|
||||
"date_of_birth": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 11,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "Steve Klabnik",
|
||||
"date_of_birth": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 12,
|
||||
"model": "searchtests.author",
|
||||
"fields": {
|
||||
"name": "Carol Nichols",
|
||||
"date_of_birth": null
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "A Game of Thrones",
|
||||
"authors": [1],
|
||||
"publication_date": "1996-08-01",
|
||||
"number_of_pages": 694
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "A Clash of Kings",
|
||||
"authors": [1],
|
||||
"publication_date": "1998-01-01",
|
||||
"number_of_pages": 768
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 3,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "A Storm of Swords",
|
||||
"authors": [1],
|
||||
"publication_date": "2000-01-01",
|
||||
"number_of_pages": 973
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 4,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "The Fellowship of the Ring",
|
||||
"authors": [2],
|
||||
"publication_date": "1954-07-29",
|
||||
"number_of_pages": 423
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 5,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "The Two Towers",
|
||||
"authors": [2],
|
||||
"publication_date": "1954-11-11",
|
||||
"number_of_pages": 352
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 6,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "The Return of the King",
|
||||
"authors": [2],
|
||||
"publication_date": "1955-10-20",
|
||||
"number_of_pages": 440
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 7,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "The Hobbit",
|
||||
"authors": [2],
|
||||
"publication_date": "1937-09-21",
|
||||
"number_of_pages": 320
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 10,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "Foundation",
|
||||
"authors": [4],
|
||||
"publication_date": "1951-01-01",
|
||||
"number_of_pages": 255
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 11,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "Learning Python",
|
||||
"authors": [5, 6],
|
||||
"publication_date": "1999-01-01",
|
||||
"number_of_pages": 1160
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 12,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "Two Scoops of Django 1.11",
|
||||
"authors": [7, 8],
|
||||
"publication_date": "2017-04-26",
|
||||
"number_of_pages": 556
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 13,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "JavaScript: The good parts",
|
||||
"authors": [9],
|
||||
"publication_date": "2008-05-18",
|
||||
"number_of_pages": 172
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 14,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "JavaScript: The Definitive Guide",
|
||||
"authors": [10],
|
||||
"publication_date": "1998-06-11",
|
||||
"number_of_pages": 792
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 15,
|
||||
"model": "searchtests.book",
|
||||
"fields": {
|
||||
"title": "The Rust Programming Language",
|
||||
"authors": [11, 12],
|
||||
"publication_date": "2018-05-22",
|
||||
"number_of_pages": 488
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Daenerys Targaryen",
|
||||
"novel": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Jon Snow",
|
||||
"novel": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 3,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Eddard Stark",
|
||||
"novel": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 4,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Daenerys Targaryen",
|
||||
"novel": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 5,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Jon Snow",
|
||||
"novel": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 6,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Daenerys Targaryen",
|
||||
"novel": 3
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 7,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Jon Snow",
|
||||
"novel": 3
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 8,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Frodo Baggins",
|
||||
"novel": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 9,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Bilbo Baggins",
|
||||
"novel": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 10,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Gandalf",
|
||||
"novel": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 11,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Frodo Baggins",
|
||||
"novel": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 12,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Bilbo Baggins",
|
||||
"novel": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 13,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Gandalf",
|
||||
"novel": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 14,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Frodo Baggins",
|
||||
"novel": 6
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 15,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Bilbo Baggins",
|
||||
"novel": 6
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 16,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Gandalf",
|
||||
"novel": 6
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 17,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Bilbo Baggins",
|
||||
"novel": 7
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 18,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Thorin Oakenshield",
|
||||
"novel": 7
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 19,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Hari Seldon",
|
||||
"novel": 10
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 20,
|
||||
"model": "searchtests.character",
|
||||
"fields": {
|
||||
"name": "Salvor Hardin",
|
||||
"novel": 10
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "searchtests.novel",
|
||||
"fields": {
|
||||
"setting": "Westeros",
|
||||
"protagonist": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "searchtests.novel",
|
||||
"fields": {
|
||||
"setting": "Westeros",
|
||||
"protagonist": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 3,
|
||||
"model": "searchtests.novel",
|
||||
"fields": {
|
||||
"setting": "Westeros",
|
||||
"protagonist": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 4,
|
||||
"model": "searchtests.novel",
|
||||
"fields": {
|
||||
"setting": "Middle Earth",
|
||||
"protagonist": 8
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 5,
|
||||
"model": "searchtests.novel",
|
||||
"fields": {
|
||||
"setting": "Middle Earth",
|
||||
"protagonist": 11
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 6,
|
||||
"model": "searchtests.novel",
|
||||
"fields": {
|
||||
"setting": "Middle Earth",
|
||||
"protagonist": 14
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 7,
|
||||
"model": "searchtests.novel",
|
||||
"fields": {
|
||||
"setting": "Middle Earth",
|
||||
"protagonist": 17
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 10,
|
||||
"model": "searchtests.novel",
|
||||
"fields": {
|
||||
"setting": "Galactic Empire",
|
||||
"protagonist": null
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"pk": 11,
|
||||
"model": "searchtests.programmingguide",
|
||||
"fields": {
|
||||
"programming_language": "py"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 12,
|
||||
"model": "searchtests.programmingguide",
|
||||
"fields": {
|
||||
"programming_language": "py"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 13,
|
||||
"model": "searchtests.programmingguide",
|
||||
"fields": {
|
||||
"programming_language": "js"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 14,
|
||||
"model": "searchtests.programmingguide",
|
||||
"fields": {
|
||||
"programming_language": "js"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 15,
|
||||
"model": "searchtests.programmingguide",
|
||||
"fields": {
|
||||
"programming_language": "rs"
|
||||
}
|
||||
}
|
||||
]
|
|
@ -1,11 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.1 on 2016-02-12 10:55
|
||||
# Generated by Django 1.11.6 on 2017-10-18 12:59
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import taggit.managers
|
||||
from django.db import migrations, models
|
||||
|
||||
import wagtail.wagtailsearch.index
|
||||
|
||||
|
||||
|
@ -14,35 +13,66 @@ class Migration(migrations.Migration):
|
|||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('wagtailcore', '0024_alter_page_content_type_on_delete_behaviour'),
|
||||
('taggit', '0002_auto_20150616_2121'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='SearchTest',
|
||||
name='Author',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=255)),
|
||||
('date_of_birth', models.DateField(null=True)),
|
||||
],
|
||||
bases=(models.Model, wagtail.wagtailsearch.index.Indexed),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Book',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=255)),
|
||||
('content', models.TextField()),
|
||||
('live', models.BooleanField(default=False)),
|
||||
('published_date', models.DateField(null=True)),
|
||||
('publication_date', models.DateField()),
|
||||
('number_of_pages', models.IntegerField()),
|
||||
],
|
||||
bases=(wagtail.wagtailsearch.index.Indexed, models.Model),
|
||||
bases=(models.Model, wagtail.wagtailsearch.index.Indexed),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='SearchTestChild',
|
||||
name='Character',
|
||||
fields=[
|
||||
('searchtest_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='searchtests.SearchTest')),
|
||||
('subtitle', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('extra_content', models.TextField()),
|
||||
('page', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='wagtailcore.Page')),
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=255)),
|
||||
],
|
||||
bases=('searchtests.searchtest',),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Novel',
|
||||
fields=[
|
||||
('book_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='searchtests.Book')),
|
||||
('setting', models.CharField(max_length=255)),
|
||||
('protagonist', models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='+', to='searchtests.Character')),
|
||||
],
|
||||
bases=('searchtests.book',),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='ProgrammingGuide',
|
||||
fields=[
|
||||
('book_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='searchtests.Book')),
|
||||
('programming_language', models.CharField(choices=[('py', 'Python'), ('js', 'Javascript'), ('rs', 'Rust')], max_length=255)),
|
||||
],
|
||||
bases=('searchtests.book',),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='searchtest',
|
||||
model_name='book',
|
||||
name='authors',
|
||||
field=models.ManyToManyField(related_name='books', to='searchtests.Author'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='book',
|
||||
name='tags',
|
||||
field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='character',
|
||||
name='novel',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='characters', to='searchtests.Novel'),
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10 on 2016-08-25 15:17
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('searchtests', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='AnotherSearchTestChild',
|
||||
fields=[
|
||||
('searchtest_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='searchtests.SearchTest')),
|
||||
('subtitle', models.CharField(blank=True, max_length=255, null=True)),
|
||||
],
|
||||
bases=('searchtests.searchtest',),
|
||||
),
|
||||
]
|
|
@ -1,22 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('searchtests', '0002_anothersearchtestchild'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='SearchTestSubObject',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('parent', models.ForeignKey(to='searchtests.SearchTest', related_name='subobjects', on_delete=models.deletion.CASCADE)),
|
||||
('name', models.CharField(max_length=255)),
|
||||
],
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
|
@ -6,87 +6,104 @@ from taggit.managers import TaggableManager
|
|||
from wagtail.wagtailsearch import index
|
||||
|
||||
|
||||
class SearchTest(index.Indexed, models.Model):
|
||||
class Author(index.Indexed, models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
date_of_birth = models.DateField(null=True)
|
||||
|
||||
search_fields = [
|
||||
index.SearchField('name'),
|
||||
index.FilterField('date_of_birth'),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Book(index.Indexed, models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
content = models.TextField()
|
||||
live = models.BooleanField(default=False)
|
||||
published_date = models.DateField(null=True)
|
||||
authors = models.ManyToManyField(Author, related_name='books')
|
||||
publication_date = models.DateField()
|
||||
number_of_pages = models.IntegerField()
|
||||
tags = TaggableManager()
|
||||
|
||||
search_fields = [
|
||||
index.SearchField('title', partial_match=True),
|
||||
index.SearchField('title', partial_match=True, boost=2.0),
|
||||
index.FilterField('title'),
|
||||
index.RelatedFields('authors', Author.search_fields),
|
||||
index.FilterField('publication_date'),
|
||||
index.FilterField('number_of_pages'),
|
||||
index.RelatedFields('tags', [
|
||||
index.SearchField('name', partial_match=True),
|
||||
index.SearchField('name'),
|
||||
index.FilterField('slug'),
|
||||
]),
|
||||
index.RelatedFields('subobjects', [
|
||||
index.SearchField('name', partial_match=True),
|
||||
]),
|
||||
index.SearchField('content', boost=2),
|
||||
index.SearchField('callable_indexed_field'),
|
||||
index.FilterField('title'),
|
||||
index.FilterField('live'),
|
||||
index.FilterField('published_date'),
|
||||
]
|
||||
|
||||
def callable_indexed_field(self):
|
||||
return "Callable"
|
||||
|
||||
@classmethod
|
||||
def get_indexed_objects(cls):
|
||||
indexed_objects = super(SearchTest, cls).get_indexed_objects()
|
||||
indexed_objects = super(Book, cls).get_indexed_objects()
|
||||
|
||||
# Exclude SearchTests that have a SearchTestChild to stop update_index creating duplicates
|
||||
if cls is SearchTest:
|
||||
# Don't index books using Book class that they have a more specific type
|
||||
if cls is Book:
|
||||
indexed_objects = indexed_objects.exclude(
|
||||
id__in=SearchTestChild.objects.all().values_list('searchtest_ptr_id', flat=True)
|
||||
id__in=Novel.objects.values_list('book_ptr_id', flat=True)
|
||||
)
|
||||
|
||||
# Exclude SearchTests that have the title "Don't index me!"
|
||||
indexed_objects = indexed_objects.exclude(
|
||||
id__in=ProgrammingGuide.objects.values_list('book_ptr_id', flat=True)
|
||||
)
|
||||
|
||||
# Exclude Books that have the title "Don't index me!"
|
||||
indexed_objects = indexed_objects.exclude(title="Don't index me!")
|
||||
|
||||
return indexed_objects
|
||||
|
||||
def get_indexed_instance(self):
|
||||
# Check if there is a SearchTestChild that descends from this
|
||||
child = SearchTestChild.objects.filter(searchtest_ptr_id=self.id).first()
|
||||
# Check if this object is a Novel or ProgrammingGuide and return the specific object
|
||||
novel = Novel.objects.filter(book_ptr_id=self.id).first()
|
||||
programming_guide = ProgrammingGuide.objects.filter(book_ptr_id=self.id).first()
|
||||
|
||||
# Return the child if there is one, otherwise return self
|
||||
return child or self
|
||||
# Return the novel/programming guide object if there is one, otherwise return self
|
||||
return novel or programming_guide or self
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
class SearchTestChild(SearchTest):
|
||||
subtitle = models.CharField(max_length=255, null=True, blank=True)
|
||||
extra_content = models.TextField()
|
||||
page = models.ForeignKey('wagtailcore.Page', null=True, blank=True, on_delete=models.SET_NULL)
|
||||
class Character(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
novel = models.ForeignKey('Novel', related_name='characters')
|
||||
|
||||
search_fields = SearchTest.search_fields + [
|
||||
index.SearchField('subtitle', partial_match=True),
|
||||
index.SearchField('extra_content'),
|
||||
index.RelatedFields('page', [
|
||||
index.SearchField('title', partial_match=True),
|
||||
index.SearchField('search_description'),
|
||||
index.FilterField('live'),
|
||||
search_fields = [
|
||||
index.SearchField('name'),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Novel(Book):
|
||||
setting = models.CharField(max_length=255)
|
||||
protagonist = models.OneToOneField(Character, related_name='+', null=True)
|
||||
|
||||
search_fields = Book.search_fields + [
|
||||
index.SearchField('setting', partial_match=True),
|
||||
index.RelatedFields('characters', [
|
||||
index.SearchField('name', boost=0.25),
|
||||
]),
|
||||
index.RelatedFields('protagonist', [
|
||||
index.SearchField('name', boost=0.5),
|
||||
]),
|
||||
]
|
||||
|
||||
|
||||
class AnotherSearchTestChild(SearchTest):
|
||||
# Checks that having the same field name in two child models with different
|
||||
# search configuration doesn't give an error
|
||||
subtitle = models.CharField(max_length=255, null=True, blank=True)
|
||||
class ProgrammingGuide(Book):
|
||||
programming_language = models.CharField(max_length=255, choices=[
|
||||
('py', "Python"),
|
||||
('js', "Javascript"),
|
||||
('rs', "Rust"),
|
||||
])
|
||||
|
||||
search_fields = SearchTest.search_fields + [
|
||||
index.SearchField('subtitle', boost=10),
|
||||
search_fields = Book.search_fields + [
|
||||
index.SearchField('get_programming_language_display'),
|
||||
index.FilterField('programming_language'),
|
||||
]
|
||||
|
||||
|
||||
class SearchTestSubObject(models.Model):
|
||||
parent = models.ForeignKey(SearchTest, related_name='subobjects', on_delete=models.CASCADE)
|
||||
name = models.CharField(max_length=255)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
|
Ładowanie…
Reference in New Issue