私は 2 つの異なるページ モデル (サブクラス化なし、「モデル名」、「id」、「パーツ」などの共通フィールドのみを持つ個別のアプリ) を持っています。たとえば、車とオートバイです。部品の表を含む別のページを作成しています (これには、pk であると想定される「id」、「Web ストア リンク」、およびすべての自転車と車を表示する「使用者」などの列も含める必要があります)。部品を使用するモデル); いくつかの同じパーツを共有できるので、(「CarPageTag」と「BikePageTag」ではなく) 同じタグ モデルに接続する必要があります。私がこれまでに試したこと:
- 「部品」の別アプリを作ってみました。他のアプリからそのクラスを含める方法を考え出しましたが、このエラーのため、車またはオートバイのいずれかで動作しますが、両方では動作しません:
AssertionError: ParentalKey(['CarsBlogApp.CarDetailPage', 'BikesBlogApp.BikeDetailPage']) が無効です。ForeignKey の最初のパラメーターは、モデル、モデル名、または文字列「self」のいずれかでなければなりません
- ManyToManyField を介してプレーンな Django アプリで動作する簡単なソリューションがありました (ただし、管理者に wagtail 自動入力タグ選択ページが必要です)
- 私はdjango、wagtail、taggitのドキュメントをすべて見ました
- 私はすべてのYouTubeチュートリアルに目を通しました
編集:私が思ったようにmodels.pyを追加するとうまくいく:
PartsApp/models.py:
from django.db import models
from wagtail.core.models import Page
from wagtail.admin.edit_handlers import FieldPanel
from modelcluster.fields import ParentalKey
from taggit.models import TaggedItemBase
class PartsPage(Page):
templates = "parts/parts_page.html"
subpage_types = []
max_count = 1
parent_page_type = ['home.HomePage']
paragraph = models.CharField(
max_length=100,
blank=False,
null=False,
help_text='Overwrites the default title',
)
content_panels = Page.content_panels + [
FieldPanel("paragraph"),
]
class Meta:
verbose_name = "Needed supplies Page"
verbose_name_plural = "Needed supplies Pages"
class PartTagPage(TaggedItemBase):
content_object = ParentalKey(
['CarApp.CarDetailPage','BikeApp.BikeDetailPage'],
related_name='tagged_items',
on_delete=models.CASCADE,
)
そしてCarsApp/models.py :
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtail.images.edit_handlers import ImageChooserPanel
from blocks import blocks
from modelcluster.fields import ParentalKey
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase
from PartsApp.models import PartTagPage
class CarListingPage(Page):
template = "CarsApp/car_listing_page.html"
subpage_types = ['CarsApp.CarDetailPage']
parent_page_type = ['home.HomePage']
max_count = 1
paragraph = models.CharField(
max_length=100,
blank=False,
null=False,
help_text='Overwrites the default title',
)
content_panels = Page.content_panels + [
FieldPanel("paragraph"),
]
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
all_cars = CarsDetailPage.objects.live().public().order_by('-first_published_at')
if request.GET.get('tag', None):
tags = request.GET.get('tag')
all_cars = all_cars.filter(tags__slug__in=[tags])
context["cars"] = all_animals
return context
class CarDetailPage(Page):
subpage_types = []
parent_page_types = ['CarsApp.CarsListingPage']
tags = ClusterTaggableManager(through='PartsApp.PartTagPage', blank=True)
name = models.CharField(
max_length=100,
blank=False,
null=False,
)
model = models.CharField(
max_length=100,
blank=False,
null=False,
)
car_image = models.ForeignKey(
"wagtailimages.Image",
blank=False,
null=True,
related_name="+",
on_delete=models.SET_NULL,
)
description = models.TextField(max_length=10000)
content_panels = Page.content_panels + [
FieldPanel("model"),
FieldPanel("name"),
FieldPanel("description"),
FieldPanel("tags"),
ImageChooserPanel("car_image"),
]
BikesApp /models.py はほとんど同じです。 繰り返しますが、これらは機能しません。