简体   繁体   English

Django model 未通过外键关系使用另一个 model

[英]Django model not using another model via a foreign key relationship

I'm trying to create a web app that will allow booking onto training sessions in a running club.我正在尝试创建一个 web 应用程序,该应用程序将允许预订跑步俱乐部的培训课程。

I've got my users app and a training_sessions app where coaches can post sessions.我有我的用户应用程序和一个 training_sessions 应用程序,教练可以在其中发布课程。 They include location, date, the coach taking the session and some details on what the session entails.它们包括位置、日期、乘坐 session 的教练以及有关 session 的一些细节。 I now want to add a facility where a logged in user can click on the session and have them booked onto it.我现在想添加一个设施,登录用户可以点击 session 并让他们预订。

I've a ClubSession model:我有一个 ClubSession model:

class ClubSession(models.Model):
    location = models.CharField(max_length=200)
    coach = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    date = models.DateTimeField(default=now)
    details = models.TextField()

    def __str__(self):
        return self.location

    def get_absolute_url(self):
        return reverse('session_detail', args=[str(self.id)])

From it's used in several views to create, edit, delete, view and list all sessions:在多个视图中使用它来创建、编辑、删除、查看和列出所有会话:

class SessionListView(ListView):
    model = ClubSession
    template_name = 'club_sessions.html'
    context_object_name = 'all_club_sessions_list'


class SessionDetailView(DetailView):
    model = ClubSession
    template_name = 'session_detail.html'
 context_object_name = 'club_session'


class SessionCreateView(CreateView):
    model = ClubSession
    template_name = 'session_new.html'
    fields = ['location', 'coach', 'date', 'details']


class SessionUpdateView(UpdateView):
    model = ClubSession
    template_name = 'session_edit.html'
    fields = ['location', 'coach', 'date', 'details']


class SessionDeleteView(DeleteView):
    model = ClubSession
    template_name = 'session_delete.html'
    success_url = reverse_lazy('home')

I'm now trying to create a booking model with a foreign key to my user and ClubSessions models.我现在正在尝试使用我的用户和 ClubSessions 模型的外键创建预订 model。 This is what I've got:这就是我所拥有的:

class ClubSessionBookings(models.Model):
    athlete = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    club_sessions = models.ForeignKey(ClubSession, on_delete=models.CASCADE)

    def __str__(self):
        return self.athlete

    def get_absolute_url(self):
        return reverse('booking_session_detail', args=[str(self.id)])

I added this view:我添加了这个视图:

class SessionBookingCreateView(CreateView):
    model = ClubSessionBookings
    template_name = 'session_booking_new.html'
    fields = ['athlete', 'location', 'coach', 'date', 'details']
    context_object_name = 'session_booking_new'

and this url:还有这个 url:

path('club_sessions_booking/', SessionBookingCreateView.as_view(), name='session_booking_new')

but I see this exception: Unknown field(s) (coach, date, location, details) specified for ClubSessionBookings .但我看到了这个例外: Unknown field(s) (coach, date, location, details) specified for ClubSessionBookings I guess there's an issue with how my ClubSessionBookings model uses ClubSession as a foreign key.我想我的 ClubSessionBookings model 如何使用 ClubSession 作为外键存在问题。 What have I done wrong and how do I resolve it?我做错了什么,我该如何解决?

Your ClubSessionBooking model has only two fields: athlete and club_sessions .您的 ClubSessionBooking model 只有两个字段: athleteclub_sessions So you are specifying wrong set of fields in SessionBookingCreateView.因此,您在 SessionBookingCreateView 中指定了错误的字段集。

If you just use these two correct fields, it should work.如果您只使用这两个正确的字段,它应该可以工作。

But in these kind of cases, we like to put a button below the sessionDetailView like 'Register for this Session'.但在这种情况下,我们喜欢在sessionDetailView下方放置一个按钮,例如“注册此会话”。 And when a logged in user clicks, the model is created automatically.当登录用户点击时,model 会自动创建。 For that maybe you collect the session Id from button click URL and logged in user from session.为此,也许您从按钮单击 URL 并从 session 登录用户收集 session Id。 Then create the model on the fly instead of using a form.然后即时创建 model 而不是使用表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM