[英](Django) UpdateView query only working sometimes
I have a school management app that has a feature to drop a student or teacher from a particular course.我有一个学校管理应用程序,它具有从特定课程中删除学生或教师的功能。 The problem is it only works with some users and not others.
问题是它只适用于某些用户而不适用于其他用户。
In my urls.py, I have the following:在我的 urls.py 中,我有以下内容:
path('profile/<int:pk>/drop_session/<int:pk2>', user_views.DropStudentFromSession.as_view(), name='drop_student_session'),
Then in my views.py, this is my CBV for dropping a student:然后在我的views.py中,这是我放弃学生的CBV:
class DropStudentFromSession(UserPassesTestMixin, UpdateView):
model = Session
template_name = 'users/drop_student_session.html'
fields = []
def test_func(self):
return True if self.request.user.groups.filter(name='Admin').count() > 0 else False
def handle_no_permission(self):
return redirect('denied')
def form_valid(self, form):
course = get_object_or_404(Session, pk=self.kwargs.get('pk2'))
user = get_object_or_404(User, pk=self.kwargs.get('pk'))
user.student.enrolled_courses.remove(course)
user.save()
return super().form_valid(form)
def get_success_url(self):
user = get_object_or_404(User, pk=self.kwargs.get('pk'))
messages.success(self.request, 'Student withdrawn from session.')
return reverse('user_profile', kwargs={'pk': user.id})
So the in the form_valid
method, it grabs the user's PK and the session's PK so it can successfully remove it from the student's enrolled courses.因此在
form_valid
方法中,它会抓取用户的 PK 和会话的 PK,因此它可以成功地将其从学生注册的课程中删除。 But it seemed to only work with my one test user that has a PK=2, but not the others.但它似乎只适用于我的一个 PK=2 的测试用户,而不适用于其他用户。
What am I missing here?我在这里想念什么?
EDIT, here's the models for Michael Lindsay:编辑,这是 Michael Lindsay 的模型:
class Student(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
enrolled_courses = models.ManyToManyField(Session, blank=True)
student_id_number = models.IntegerField(null=True, blank=True)
def __str__(self):
return f'{self.user.last_name}, {self.user.first_name}'
And here's the Session model:这是 Session model:
class Session(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
course_date_start = models.DateField()
course_date_end = models.DateField()
def session_id(self):
new_session_date = self.course_date_start.strftime('%Y')
return f'{new_session_date}{self.course.number}{self.pk}'
def __str__(self):
return f'{self.course.number} - {self.course.title} - {self.session_id()}'
Thanks to @MichaelLindsay, I used a RedirectView instead so now the code looks like this:感谢@MichaelLindsay,我改用了 RedirectView,所以现在代码如下所示:
class DropSessionStudent(UserPassesTestMixin, RedirectView):
permanent = False
query_string = True
pattern_name = 'user_profile'
def test_func(self):
return True if self.request.user.groups.filter(name='Admin').count() > 0 else False
def handle_no_permission(self):
return redirect('denied')
def get_redirect_url(self, *args, **kwargs):
user = get_object_or_404(User, pk=self.kwargs.get('pk'))
course = get_object_or_404(Session, pk=self.kwargs.get('pk2'))
user.student.enrolled_courses.remove(course)
user.save()
messages.success(self.request, 'Session successfully withdrawn from the student.')
return reverse('user_profile', kwargs={'pk': user.id})
The only downside to this is that a misclick will drop a student or teacher from that class, so I need to add a confirm dialog, but now it's working.唯一的缺点是点击错误会从 class 中删除学生或老师,所以我需要添加一个确认对话框,但现在它正在工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.