第二页 = ParentalKey() 定义,在不同的页面模型中使用一个片段
Second page = ParentalKey() definition, to use one snippen in different Page models
我定义了两个 Relationship(Orderable, models.Model)
以便能够在不同的页面模型中使用一个模型片段,例如:
class GroupstageTournamentModel(models.Model):
...
class GroupstageTournamentRelationship(Orderable, models.Model):
page = ParentalKey('TournamentPage',
related_name='groupstage_tournament_relationship')
match = models.ForeignKey('GroupstageTournamentModel',
related_name='match_tournament_relationship')
panels = [
PageChooserPanel('match')
]
class MatchesScreencastRelationship(Orderable, models.Model):
page = ParentalKey('ScreencastPage',
related_name='groupstage_screencast_relationship')
match = models.ForeignKey('GroupstageTournamentModel',
default="", related_name='match_screen_relationship')
panels = [
PageChooserPanel('match')
]
class TournamentPage(Page):
starts_at = models.DateTimeField(blank=True)
ends_at = models.DateTimeField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('title'),
FieldPanel('starts_at'),
FieldPanel('ends_at'),
InlinePanel(
'groupstage_tournament_relationship', label="Group game:",
panels=None, min_num=1),
]
def __str__(self):
return self.title
class ScreencastPage(Page):
content_panels = Page.content_panels + [
FieldPanel('title'),
InlinePanel(
'groupstage_screencast_relationship', label="Playing First",
panels=None, max_num=1),
]
parent_page_types = ['home.HomePage']
subpage_types = []
def __str__(self):
# return self.title
return '{} \n Nächste: {}'.format(self.groupstage_relationship, self.final_phase_relationship)
如您所见,我的想法是在站点 TournamentPage
中使用其中一个,在 ScreencastPage
中使用另一个。如果我这样做,我会得到这个错误:
错误:
django.core.exceptions.FieldError: Local field 'id' in class 'GroupstageTournamentRelationship' clashes with field of the same name from base class 'GroupstageTournamentModel'.
如何解决这个问题?是否可以以某种方式添加与 ScreencastPage
的关系的第二个 ParentalKey,并像在 backerydemo?
中那样直接在 GroupstageTournamentModel
中使用它
更新
我将 GroupstageTournamentModel
从 model.Model
更改为 ClusterableModel
并且我更改了 related_name
以便它在两个 related_name
中都不同
related_name='groupstage_tournament_relationship'
和 related_name='groupstage_screencast_relationship'
。我再次进行了迁移并得到了同样的错误。这是我的 GroupstageTournamentRelationship
class:
class GroupstageTournamentModel(ClusterableModel):
number = models.PositiveSmallIntegerField(
help_text="Add the unique number of this Match.")
starts_at = models.DateTimeField()
# Team 1
team_1 = models.ForeignKey(
TeamRooster,
null=True, blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
team_1_dress = ColorField(default='#ff0000', blank=True)
team_1_first_halftime_score = models.PositiveSmallIntegerField(blank=True, default="")
# Team 2
team_2 = models.ForeignKey(
TeamRooster,
null=True, blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
team_2_dress = ColorField(default='#0066ff', blank=True)
team_2_first_halftime_score = models.PositiveSmallIntegerField(blank=True, default="")
panels = [
FieldPanel('number', classname="6"),
FieldPanel('starts_at', classname="6"),
MultiFieldPanel([
FieldRowPanel([
FieldPanel('team_1', classname="6"),
FieldPanel('team_1_dress', classname="6"),
FieldPanel('team_1_first_halftime_score', classname="3"),
]),
], classname="full", heading="Team 1"),
MultiFieldPanel([
FieldRowPanel([
FieldPanel('team_2', classname="6"),
FieldPanel('team_2_dress', classname="6"),
FieldPanel('team_2_first_halftime_score', classname="3"),
]),
], classname="full", heading="Team 2"),
]
def __str__(self):
return '{} vs {} {} - {}'.format(self.team_1, self.team_2, self.starts_at, self.number)
class Meta:
verbose_name = 'Gruppenphase Spiel'
verbose_name_plural = 'Gruppenphase'
更新
我在迁移文件中发现了以下内容,我不确定如果删除它会发生什么:
更新
为了清楚起见,我缩短了模型并添加了 "TournamentPage"。
我删除了包含 0001_inicial.py 的迁移文件,然后再次 py manage.py makemigrations
并且一切正常!
我定义了两个 Relationship(Orderable, models.Model)
以便能够在不同的页面模型中使用一个模型片段,例如:
class GroupstageTournamentModel(models.Model):
...
class GroupstageTournamentRelationship(Orderable, models.Model):
page = ParentalKey('TournamentPage',
related_name='groupstage_tournament_relationship')
match = models.ForeignKey('GroupstageTournamentModel',
related_name='match_tournament_relationship')
panels = [
PageChooserPanel('match')
]
class MatchesScreencastRelationship(Orderable, models.Model):
page = ParentalKey('ScreencastPage',
related_name='groupstage_screencast_relationship')
match = models.ForeignKey('GroupstageTournamentModel',
default="", related_name='match_screen_relationship')
panels = [
PageChooserPanel('match')
]
class TournamentPage(Page):
starts_at = models.DateTimeField(blank=True)
ends_at = models.DateTimeField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('title'),
FieldPanel('starts_at'),
FieldPanel('ends_at'),
InlinePanel(
'groupstage_tournament_relationship', label="Group game:",
panels=None, min_num=1),
]
def __str__(self):
return self.title
class ScreencastPage(Page):
content_panels = Page.content_panels + [
FieldPanel('title'),
InlinePanel(
'groupstage_screencast_relationship', label="Playing First",
panels=None, max_num=1),
]
parent_page_types = ['home.HomePage']
subpage_types = []
def __str__(self):
# return self.title
return '{} \n Nächste: {}'.format(self.groupstage_relationship, self.final_phase_relationship)
如您所见,我的想法是在站点 TournamentPage
中使用其中一个,在 ScreencastPage
中使用另一个。如果我这样做,我会得到这个错误:
错误:
django.core.exceptions.FieldError: Local field 'id' in class 'GroupstageTournamentRelationship' clashes with field of the same name from base class 'GroupstageTournamentModel'.
如何解决这个问题?是否可以以某种方式添加与 ScreencastPage
的关系的第二个 ParentalKey,并像在 backerydemo?
GroupstageTournamentModel
中使用它
更新
我将 GroupstageTournamentModel
从 model.Model
更改为 ClusterableModel
并且我更改了 related_name
以便它在两个 related_name
中都不同
related_name='groupstage_tournament_relationship'
和 related_name='groupstage_screencast_relationship'
。我再次进行了迁移并得到了同样的错误。这是我的 GroupstageTournamentRelationship
class:
class GroupstageTournamentModel(ClusterableModel):
number = models.PositiveSmallIntegerField(
help_text="Add the unique number of this Match.")
starts_at = models.DateTimeField()
# Team 1
team_1 = models.ForeignKey(
TeamRooster,
null=True, blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
team_1_dress = ColorField(default='#ff0000', blank=True)
team_1_first_halftime_score = models.PositiveSmallIntegerField(blank=True, default="")
# Team 2
team_2 = models.ForeignKey(
TeamRooster,
null=True, blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
team_2_dress = ColorField(default='#0066ff', blank=True)
team_2_first_halftime_score = models.PositiveSmallIntegerField(blank=True, default="")
panels = [
FieldPanel('number', classname="6"),
FieldPanel('starts_at', classname="6"),
MultiFieldPanel([
FieldRowPanel([
FieldPanel('team_1', classname="6"),
FieldPanel('team_1_dress', classname="6"),
FieldPanel('team_1_first_halftime_score', classname="3"),
]),
], classname="full", heading="Team 1"),
MultiFieldPanel([
FieldRowPanel([
FieldPanel('team_2', classname="6"),
FieldPanel('team_2_dress', classname="6"),
FieldPanel('team_2_first_halftime_score', classname="3"),
]),
], classname="full", heading="Team 2"),
]
def __str__(self):
return '{} vs {} {} - {}'.format(self.team_1, self.team_2, self.starts_at, self.number)
class Meta:
verbose_name = 'Gruppenphase Spiel'
verbose_name_plural = 'Gruppenphase'
更新
我在迁移文件中发现了以下内容,我不确定如果删除它会发生什么:
更新
为了清楚起见,我缩短了模型并添加了 "TournamentPage"。
我删除了包含 0001_inicial.py 的迁移文件,然后再次 py manage.py makemigrations
并且一切正常!