How to set a field of the model in view using generic views?

后端 未结 2 2044
余生分开走
余生分开走 2021-02-06 17:28

I have a model, which has an author ForeignKey, as such:

class Appointment(models.Model):
    # ...
    author = models.ForeignKey(User)
         


        
2条回答
  •  醉话见心
    2021-02-06 17:51

    I've modified my generic view subclass as such:

    class AppointmentCreateView(CreateView):        
        model=Appointment
        form_class = AppointmentCreateForm
    
        def post(self, request, *args, **kwargs):
            self.object = None
            form_class = self.get_form_class()
            form = self.get_form(form_class)
    
            # the actual modification of the form
            form.instance.author = request.user
    
            if form.is_valid():
                return self.form_valid(form)
            else:
                return self.form_invalid(form)
    

    There are few important parts here:

    • I modified form instance field, which holds the actual model that's going to be saved.
    • You can of course get rid of the form_class
    • The post method that I've needed to modify was two classes above in hierarchy, so I needed to incorporate the base code and the self.object = None line, merging the overload and base into one function (I'm not calling super in post).

    I think it's a good way to solve pretty common problem, and once again I don't have to write my own custom view.

提交回复
热议问题