覆盖Delete方法后,Django DeleteView不会重定向到成功URL

时间:2019-06-03 12:22:38

标签: python django django-class-based-views

我有一个学生模型,其中包含django用户模型的外键,我想在删除学生时删除关联的用户(delete=CASCADE上则相反),因此我覆盖了delete方法,但删除后success_url= '/student/'不起作用,URL仍然是例如'/students/delete/323/'并继续将pk发送到url,当我不重写delete方法时,它的工作原理就像一个魅力。任何想法?

class StudentDeleteView(DeleteView):
    model = Student
    template_name_suffix = '_confirm_delete'
    success_url = '/students/'

    def delete(self, request, *args, **kwargs):
        related_user = Student.objects.get(student_id=kwargs['pk']).user_id
        User.objects.filter(id=related_user).delete()
        return super(StudentDeleteView, self).delete(request, *args, **kwargs)


class Student(models.Model):
    student_id = models.IntegerField()
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='students')
    courses = models.ManyToManyField('Course', through='StudentCourse', related_name='students')
    classrooms = models.ManyToManyField('Classroom', through='Register', related_name='students')
    last_modified_date = models.DateTimeField(null=True)
    birth_date = models.CharField(max_length=10, null=True)
    image = models.ImageField(upload_to='profile_image', blank=True)

0 个答案:

没有答案