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

心不动则不痛 提交于 2019-12-03 15:14:48

问题


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

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

I want the author field to be set automatically when creating appointment to currently logged-in user. In other words, the author field should not appear in my Form class:

class AppointmentCreateForm(ModelForm):
    class Meta:
        model = Appointment
        exclude = ('author')

There are two problems:

  1. How to access the form in generic CreateView and set the author?
  2. How to tell the form to save the excluded field along with the values read from user input?

回答1:


The following seem slightly simpler. Note that self.request is set in View.as_view

class AppointmentCreateView(CreateView):        
    model=Appointment
    form_class = AppointmentCreateForm

    def get_form(self, form_class):
        form = super(AppointmentCreateView, self).get_form(form_class)
        # the actual modification of the form
        form.instance.author = self.request.user
        return form



回答2:


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.



来源:https://stackoverflow.com/questions/13659508/how-to-set-a-field-of-the-model-in-view-using-generic-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!