我有两个不同的页面模型(没有子类化,单独的应用程序,只有类似的字段是常见的字段,例如“模型名称”,“id”,“零件”),比方说,汽车和摩托车。我正在制作一个单独的页面,其中包含其部件的表格(还需要包含诸如“id”之类的列,我认为它可以是 pk、“网上商店链接”和“使用者”,它将显示所有自行车和汽车使用该部件的模型);
由于它们可以共享一些相同的部分,我希望它们连接到相同的标签模型(而不是“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,
)
And 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"),
]
The BikesApp/models.py 几乎相同。
Again, 这些不起作用。