如何传递来自将ModelForm提交到ManyToMany保存的用户的信息?

时间:2019-06-03 22:30:22

标签: python django

我正在尝试使用CreateView从Django ModelForm将记录插入到交叉表中,但是我不确定如何从当前登录的用户访问信息。我已经尝试过request.user,但是我不知道将其放置在视图或表单中的哪个位置,并且每次遇到相同的错误“在'school_id'列中的空值违反了非空约束”时,都会遇到同样的错误。所有其他值均正确。有人可以教我正确的方法吗?我真的很感激。

我试图把它放在我的视野中,但是没有用:

    def form_valid(self, form):
        form.instance.applicant = self.request.user
        g = Group.objects.get(name='PendingApplicants')
        g.user_set.add(self.request.user)

        for program in form.cleaned_data['programs']:
            pro = tblProgramSchool()
            pro.application_id = self.object
            pro.program = program
            pro.school = self.request.user.mainschool
            pro.save()

        return super().form_valid(form)

我以自己的形式尝试了此操作,但也没有用:

    def save(self, commit=True):
        instance = forms.ModelForm.save(self, False)

        old_save_m2m = self.save_m2m

        def save_m2m():
            old_save_m2m
            instance.programs.clear()
            for program in self.cleaned_data['programs']:
                instance.programs.add(program = program, school = self.request.user.mainschool)



            self.save_m2m = save_m2m

            instance.save()
            self.save_m2m()

            return instance

我知道我错过了一些基本知识,但是在阅读了一堆答案和Django文档之后,我感到迷失了。

models.py

class Application(models.Model):

    id = models.AutoField(primary_key=True)

    applicant = models.ForeignKey('users.CustomUser', null = False, blank = False, on_delete=models.PROTECT)

    programs = models.ManyToManyField(tblProgram, blank = True, through = tblProgramsSchool)


class tblProgramsSchool(models.Model):

    id = models.AutoField(primary_key=True)

    school = models.ForeignKey(tblSchool, on_delete=models.PROTECT)

    program = models.ForeignKey(tblProgram, on_delete=models.PROTECT)

    application = models.ForeignKey('Application', on_delete=models.PROTECT)


class tblProgram(models.Model):

    id = models.AutoField(primary_key=True)

    name = models.CharField(max_length=50, null = False, blank = False)

    description = models.CharField(max_length = 200, null = True, blank = True)


class CustomUser(AbstractUser):
    """A model for a custom user"""
...

    mainschool = models.ForeignKey(tblSchool, on_delete=models.PROTECT, null=True, blank=True)

...

forms.py

class ApplicationForm(forms.ModelForm):
    class Meta:
        model = Application

        fields = ['programs', ...
                   ]


        labels = {
            'programs': 'Please select the programs you know of', ...
        }

        help_texts = {
            'programs': 'Leave blank if there are no programs at your school'
        }

        widgets = {
            'programs': CheckboxSelectMultiple()
        }

views.py


class ApplicationView(LoginRequiredMixin, CreateView):
    form_class = ApplicationForm
    success_url = 'success'
    template_name = 'apply.html'
    login_url = 'login'

    def form_valid(self, form):
        form.instance.applicant = self.request.user
        g = Group.objects.get(name='PendingApplicants')
        g.user_set.add(self.request.user)
        return super().form_valid(form)

1 个答案:

答案 0 :(得分:0)

万一有人迷路了,我发现了问题。

这需要在基于类的视图中:

    def get_form_kwargs(self):
        kwargs = super(ApplicationView, self).get_form_kwargs()
        kwargs.update({'user': self.request.user})
        return kwargs

,并且需要以模型形式访问用户:

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(ApplicationForm, self).__init__(*args, **kwargs)

因此,可以使用它来填充关联表:

        def save_m2m():
            old_save_m2m
            instance.programs.clear()
            for program in self.cleaned_data['programs']:
                tblProgramSchool.objects.create(application_id=app.id, school_id=self.user.mainschool.id, program_id=program.id)