Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

django - Wagtail - How to save a record which uses InlinePanel containing M2M

I have a model which uses an InlinePanel to reference another model (ClubTimetable) which itself contains an M2M relationship to DayOfTheWeek. It seems as though I am unable to save the record because ClubTimetable is not yet saved and cannot create the M2M relationship to DayOfTheWeek, causing the following error:

"<ClubTimetables: Club Timetable (str) 18:00:00>" needs to have a value for field "id" before this many-to-many relationship can be used.

What can I do to solve this issue?

My models look like this

# ClubManager

@register_snippet
class ClubManager(ClusterableModel):

    name = models.CharField('Club name',
                            max_length=255,
                            help_text='The name of the club.')

    panels = [
        FieldPanel('name'),
        InlinePanel('club_timetable', heading='Timetable Information')
    ]
# ClubTimetables

class ClubTimetables(Orderable, AddressBase):

    attached_to = ParentalKey(
        'club.ClubManager', related_name='club_timetable')

    weekday = models.ManyToManyField(DaysOfTheWeek)

    start_time = models.TimeField()
    end_time = models.TimeField()

    panels = [ ... ] + AddressBase.panels
# DaysOfTheWeek

class DaysOfTheWeek(models.Model):

    weekday = models.CharField(max_length=9)

    def __str__(self):
        return self.weekday

Visually, it looks like this

(Initial) Initial view

(Adding timetable) After adding timetable


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...